Picturebox1.Image = Nothing ... Huh?

Serpwidgets

New member
Joined
Jan 2, 2007
Messages
3
Location
Wisconsin
Programming Experience
10+
I've tried searching and none of the answers here make any sense to me.

I'm moving up from VB5 (and doing graphics with the API) to .net and trying to get a grip on the graphics system. I'm simply trying to load a picture, resize it, and save it as a jpeg at a specific quality.

My test app uses the following to load and draw the graphic, which works fine:

VB.NET:
Private myBitmap As Bitmap
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2]  myBitmap = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Bitmap([/SIZE][SIZE=2][COLOR=#a31515]"C:\Documents and Settings\Owner\Desktop\Windy.bmp"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PictureBox1_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PictureBox1.Paint[/SIZE]
[SIZE=2]   e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic[/SIZE]
[SIZE=2]   e.Graphics.DrawImage(myBitmap, 0, 0, PictureBox1.Width, PictureBox1.Height)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_KeyDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.KeyEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].KeyDown[/SIZE]
[SIZE=2]   PictureBox1.Image.Save([/SIZE][SIZE=2][COLOR=#a31515]"C:\Documents and Settings\Owner\Desktop\WindyTest.jpg"[/COLOR][/SIZE][SIZE=2], Imaging.ImageFormat.Jpeg)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

...but when I try to save it tells me that the Image = Nothing. (NullreferenceException was unhandled, IOW the "image" doesn't exist.)

I've read several other threads, and the answers are always in generalities, no code... what exactly (in code) do I need to do in order to put what is in the picturebox into a file?

Also, I have not been able to figure out what reference is used to set/get the "quality/compression" level used when saving a jpeg. Can this be set to any integer between 1-100 or it is only a couple of predefined "quality" settings?

Thanks in advance. :)
 
Painting the image on top of the control through the Paint event does not put that into the Image property. If you want to display an image just set the Image property and the Picturebox will display it. Bitmap is derived from Image so you may here do this:
VB.NET:
PictureBox1.Image = myBitmap
'or
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\Owner\Desktop\Windy.bmp")
You can also save the Bitmap object directly (Bitmap.Save method is inherited from Image class):
VB.NET:
myBitmap.Save(...)
See this about Jpeg compression http://support.microsoft.com/?kbid=324788
 
Ah. So I need to load in and work with the bitmap object itself in memory, and save it from memory, and blit it to the picturebox from there. For some reason I was doing this backwards and thinking the pixel data was stored somewhere in the picturebox object. I was able to save it successfully but I'm still not clear on the relationships of the objects I'm working with. Is there a pedigree somewhere to show what comes from what? ;)

Wow, 16 lines of code just to be able to pass an integer from 0 to 100. :eek:
 
Back
Top