A Generic error occurred in GDI+

Hi experts,
I'm hoping that one of you has seen this before.  I am trying to save an image that is user-selectable in a picturebox control to a bytearray, converting it to a jpeg type (for use in the byte array).  Now if I specify a JPEG t ype image when populating the picturebox control and execute my method, then  I get the error "A Generic error occurred in GDI+".  If I use PNG when populating the picturebox then call the method, it executes just fine!  I need to be able ot save to a byte array in the JPEG format to keep the size of the data down but can't seem to get past this hurdle for some reason.
Private Function PicToJPEGByteArray(ByVal bmp As Bitmap) As Byte()

        Dim ms As New MemoryStream()
        ' Save to memory using the Jpeg format
        bmp.Save(ms, ImageFormat.Jpeg)

        ' read to end
        Dim bmpBytes As Byte() = ms.GetBuffer()
        bmp.Dispose()
        ms.Close()

        Return bmpBytes

End Function

Open in new window

LVL 1
sharizodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

x77Commented:
MemoryStream buffer is greater than content.
You need take memorystream.Length to get content Length:

   Dim bmpBytes As Byte(cint(ms.Length-1))
   Array.Copy(ms.GetBuffer, bmpBytes , bmpBytes .Length)
0
x77Commented:
Sorry,   -   Dim bmpBytes(cint(ms.Length-1))  As Byte
0
CodeCruiserCommented:
Is the error on save line? Where is the bmp coming from? Can you show us that code?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mike TomlinsonMiddle School Assistant TeacherCommented:
...and couldn't you just use MemoryStream.ToArray()?
http://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray(VS.71).aspx

    Dim bmpBytes As Byte() = ms.ToArray()
    ...
    return bmpBytes
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sharizodAuthor Commented:
Wow.  Sorry for the delay in posting!

I call the function above like this:

rootMap.Image = PicToJPEGByteArray(picMap.Image)

picMap is the picturebox I am trying to convert to the byte array and the .Image property of the rootMap object
is just the holder of the byte array.  The image property of the RootMap is declared internally as:
Public Property Image() As Byte().

Oh, and the error was occurring on the line: bmp.Save(ms, ImageFormat.Jpeg)

Thanks for your suggestions! I will try them out and report back.
0
CodeCruiserCommented:
By the way,

Public Property Image() As Byte().

does not look right to me. Aren't you declaring that as an array?

Public Property Image As Byte().
0
sharizodAuthor Commented:
I think that is just the way it shows up in the object browser (see image).  Anyway,
I don't have control over the way it is declared since it is an external third-party
developed library.
for-EE.png
0
sharizodAuthor Commented:
Brilliant!  Thanks for the suggestion Idle_Mind!  I guess that is simpler, to just take the image type that they select and not worry about conversion to jpeg before storage.  If they want the compression of jpeg, then they will have to save images they want to that format before storing in the third party software my program interfaces with.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.