Losing image on a form

PJRoyle

Member
Joined
Jun 7, 2004
Messages
11
Location
Uckfield, East Sussex, UK
Programming Experience
10+
I have a form which has a PictureBox (pbMap) containing an image, and a grid of data to the side. The image has been painted using this code:

Private mypic As System.Drawing.Bitmap
Private g As Graphics

mypic = New System.Drawing.Bitmap(strDataDir & "\" & strMapName)
g = pbMap.CreateGraphics
g.DrawImage(mypic, New Rectangle(0, 0, 640, 480))

Plus some other bits of drawing - which is why I'm not just assigning the image to the PictureBox.

As soon as I try to change the data in the grid (by keying into it) the picture disappears. I have a "Refresh picture" button, but I don't want users to have to keep pressing that. I have tried using both the KeyPress and KeyDown to trigger a redraw, but they don't work. The code is executed, but I don't see the image. Strangely, this only happens on the first attempt to modify the grid - after I lose the image once, and refresh it by the button, it stays there!

Please, has ANYONE got an answer to this? This is driving me daft!

Peter Royle
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Where are you "painting" the image? The Paint event of the pictureBox will be called anytime the pictureBoxs' graphics become invalid. This happens in many situations such as moving the pictureBox off the screen, moving the cursor over the pictureBox, other controls redrawing, .... If you aren't doing the drawing in the paint event of the pictureBox, when the paint event occurs for the pictureBox, the default graphics will be drawn (possibly a blank rectangle of the pictureBox's BackColor) and the image will disappear.

I would suggest creating the bitmap as you have done, but create the graphics object from the bitmap instead of the pictureBox. Then do the other bits of drawing via the graphics object to the bitmap and finally assign the bitmap to the Image property of the pictureBox. Here's sample code:

VB.NET:
mypic = New Bitmap(strDataDir & "\" & strMapName)
g = Graphics.FromImage(mypic)
'Plus some other bits of drawing 
pbMap.image = mypic
 

PJRoyle

Member
Joined
Jun 7, 2004
Messages
11
Location
Uckfield, East Sussex, UK
Programming Experience
10+
Brilliant reply!!

Thanks, Paszt!

That has solved the problem described, plus a couple of other related ones that I didn't mention, 'cos I suspected they were all basically caused by the same thing.

Thanks again,
Peter
 

saripar

New member
Joined
Jan 28, 2005
Messages
3
Programming Experience
1-3
I have a picture box at the center of which I need to display an image. The Image that is to be displayed will be selected by the user by clicking on a button next to the picture box (I pop up the FileOpen dlgbox). However the picture is never displayed. I think it is displayed momentarily, after the flash of the FileOpen dialog box. Here is the code that I am using to display the picture.

Any ideas why this is happening?

Dim O_picture As System.Drawing.Image
O_picture = System.Drawing.Image.FromFile(VB6.GetPath & "\Images\" & bmpImage)

Dim g As Graphics = pbox.CreateGraphics() 'Added by SP
With pbox

g.DrawImage(O_picture , CSng(.Size.Width / 2), CSng(.Size.Height / 2), 32, 32)

End With
g.Dispose()

What trial and error I have done so far

Replaced the source O_picture with another invisible picture box on the form. Since I had some doubt if O_picture was being loaded with the image correctly.

Tried out what has been suggested in this thread

It works, but now I am not sure how to center the picture on the picture box. Also sometimes the new picture I select appears on the picture and sometimes the old picture remains.

Dim O_picture As System.Drawing.Image
O_picture = System.Drawing.Image.FromFile(VB6.GetPath & "\Images\" & OImage)

Dim g As Graphics = Graphics.FromImage(O_picture)
pbox.Image = O_picture
g.Dispose()

Any help would be appreciated.

Thanks in advance!
 
Last edited:
Top Bottom