PictureBox Image.Save Error

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 52
Reputation: bwkeller is an unknown quantity at this point 
Solved Threads: 6
bwkeller bwkeller is offline Offline
Junior Poster in Training

PictureBox Image.Save Error

 
0
  #1
Mar 7th, 2008
Just for the heck of it I'm building a graphic editor, mainly to see how far I can take it and how much I can learn as I go. I'm having a problem with Image.Save.

First I have an MDI parent form from which graphic files can be opened. After selecting the file in an OPenDialog (ODialog) I create an instance of a form and load the picture into the Picturebox the form contains:

VisualBasic Syntax (Toggle Plain Text)
  1. frmPic = New frmPicture
  2. frmPic.Text = ODialog.SafeFileName
  3. frmPic.SavedFullName = ODialog.FileName
  4. frmPic.MdiParent = Me
  5. frmPic.PictureBox1.Load(ODialog.FileName)
  6. frmPic.Show()

Later on when I need to use a color blotter type function I am converting the picturebox image into a stream and saving it to a bitmap so that I can use the GetPixel function to pull the color under the mouse. The strange thing is it only seems to work for PNG files. Every other type of file is giving me the "A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
A generic error occurred in GDI+." error message, which the documentation says means, "The image was saved with the wrong image format."

The code looks like this:

VisualBasic Syntax (Toggle Plain Text)
  1. Select Case Me.Text.Substring(Text.LastIndexOf(".") + 1).ToLower
  2. Case "bmp"
  3. TheFmt = ImageFormat.Bmp
  4. Case "gif"
  5. TheFmt = ImageFormat.Gif
  6. Case "jpg"
  7. TheFmt = ImageFormat.Jpeg
  8. Case "png"
  9. TheFmt = ImageFormat.Png
  10. Case "wmf"
  11. TheFmt = ImageFormat.Wmf
  12. End Select
  13. Try
  14. PictureBox1.Image.Save(TheStream, TheFmt)
  15. ThePicture = Bitmap.FromStream(TheStream)
  16. Catch ex As Exception
  17. Debug.Print(ex.Message)
  18. End Try

The Select Case does generate the correct ImageFormat. The error is generated on the PictureBox1.Image.Save(TheStream, TheFmt) line, but like I said, works as advertised if I use a PNG file.

I guess I am looking for anyone who might have a clue as to the problem or knows another (better!, faster!) way of getting the color of a pixel out of a picturebox.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 52
Reputation: bwkeller is an unknown quantity at this point 
Solved Threads: 6
bwkeller bwkeller is offline Offline
Junior Poster in Training

Re: PictureBox Image.Save Error

 
0
  #2
Mar 7th, 2008
Well, I just tried to skip the conversion to a memorystream altogether and replaced

VisualBasic Syntax (Toggle Plain Text)
  1. PictureBox1.Image.Save(TheStream, TheFmt)
  2. ThePicture = Bitmap.FromStream(TheStream)

with

VisualBasic Syntax (Toggle Plain Text)
  1. ThePicture = PictureBox1.Image

and it worked. This is what usually happens when I ask for help, I figure it out shortly thereafter.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC