Results 1 to 10 of 10

Thread: [RESOLVED] Why does this not draw an image ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] Why does this not draw an image ?

    Hi,

    I can't see why this does not draw an image on my form.
    I expect it's something basic but I can't see what.
    I've checked that 'img' does in fact contain a 'System Drawing Bitmap'.

    vb.net Code:
    1. Imports System.Drawing.Imaging
    2. Imports System.IO
    3.  
    4. Public Class Form1
    5.  
    6.     Dim Img As Image = New Bitmap(Me.GetType(), "Ring80BW.png")
    7.  
    8.     Private Sub Form1_Load() Handles MyBase.Load
    9.         DrawPic()
    10.     End Sub
    11.  
    12.     Public Sub DrawPic()
    13.         Dim destRect As New Rectangle(20, 20, 200, 200)
    14.         Using gr As Graphics = Graphics.FromImage(Img)
    15.             gr.DrawImage(Img, destRect)
    16.         End Using
    17.     End Sub
    18.  
    19. End Class

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Why does this not draw an image ?

    You aren't drawing anything on the form, so you see nothing on the form. You're just drawing an Image on itself:
    Code:
    Using gr As Graphics = Graphics.FromImage(Img)
        gr.DrawImage(Img, destRect)
    End Using
    If you want to draw on the form then you should be handling the form's Paint event and using the Graphics object the event handler provides.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Why does this not draw an image ?

    Also, that seems a slightly odd way to get an image resource. If this is a resource that you're adding yourself, is there a reason that you're not just using My.Resources.Ring80BW?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Why does this not draw an image ?

    Why are you omitting proper events signatures? They’re not optional...

    Code:
    imports System.Drawing.Imaging
    Imports System.IO
     
    Public Class Form1
     
        Dim Img As New Bitmap("Ring80BW.png")
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           Me.Refresh
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            DrawPic(e.Graphics)
        End Sub
     
        Public Sub DrawPic(gr1 As Graphics)
            Dim destRect As New Rectangle(20, 20, 200, 200)
            Using gr2 As Graphics = Graphics.FromImage(Img)
                gr2.DrawRectangle(Pens.Black, destRect)
            End Using
            gr1.DrawImage(img, Point.Empty)
        End Sub
     
    End Class

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Why does this not draw an image ?

    Quote Originally Posted by .paul. View Post
    Why are you omitting proper events signatures? They’re not optional...
    https://www.vbforums.com/showthread....an-observation

    ...

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Why does this not draw an image ?

    Quote Originally Posted by OptionBase1 View Post
    Perhaps he can get away with it... Doesn't mean he should

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why does this not draw an image ?

    Quote Originally Posted by .paul. View Post
    Perhaps he can get away with it... Doesn't mean he should
    I've done it that way since VS2005 at which time it was recommended by someone in this forum.


    Pop
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Why does this not draw an image ?

    You could draw it in the Paint event, or if you're not going to change it once the form is displayed, you could assign it to the background image of the form.
    Code:
        Imports System.Drawing.Imaging
        Imports System.IO
         
        Public Class Form1
         
            Dim Img As Image = New Bitmap(Me.GetType(), "Ring80BW.png")
         
            Private Sub Form1_Load() Handles MyBase.Load
                DrawPic()
                Me.BackgroundImage = img
            End Sub
         
            Public Sub DrawPic()
                Dim destRect As New Rectangle(20, 20, 200, 200)
                Using gr As Graphics = Graphics.FromImage(Img)
                    gr.DrawImage(Img, destRect)
                End Using
            End Sub
         
        End Class
    You could also change it after the form is shown if you wanted to by updating the img and refreshing the form.
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using gr As Graphics = Graphics.FromImage(img)
                gr.DrawRectangle(Pens.Red, 40, 40, 320, 320)
            End Using
            Me.Refresh()
        End Sub
    Since the form's background image is referencing img, if you update img and refresh the form it refreshes the background from that img without the need of a paint event handler.
    Last edited by passel; Mar 17th, 2021 at 06:15 AM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why does this not draw an image ?

    Quote Originally Posted by jmcilhinney View Post
    Also, that seems a slightly odd way to get an image resource. If this is a resource that you're adding yourself, is there a reason that you're not just using My.Resources.Ring80BW?
    I did it that way (My.Resources.Ring80BW) but I couldn't see it in Watch, so I used the code you're asking about... Then I realised I was looking at the wrong variable, but was too engrossed to change it back.
    Along with the sunshine there has to be a little rain sometime.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why does this not draw an image ?

    Ok, Thanks guys, that's fixed it.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width