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:

frmPic = New frmPicture
        frmPic.Text = ODialog.SafeFileName
        frmPic.SavedFullName = ODialog.FileName
        frmPic.MdiParent = Me

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:

Select Case Me.Text.Substring(Text.LastIndexOf(".") + 1).ToLower
                Case "bmp"
                    TheFmt = ImageFormat.Bmp
                Case "gif"
                    TheFmt = ImageFormat.Gif
                Case "jpg"
                    TheFmt = ImageFormat.Jpeg
                Case "png"
                    TheFmt = ImageFormat.Png
                Case "wmf"
                    TheFmt = ImageFormat.Wmf
            End Select
                PictureBox1.Image.Save(TheStream, TheFmt)
                ThePicture = Bitmap.FromStream(TheStream)
            Catch ex As Exception
            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.

Well, I just tried to skip the conversion to a memorystream altogether and replaced

PictureBox1.Image.Save(TheStream, TheFmt)
                ThePicture = Bitmap.FromStream(TheStream)


ThePicture = PictureBox1.Image

and it worked. This is what usually happens when I ask for help, I figure it out shortly thereafter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.