[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

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

0
sharizod
Asked:
sharizod
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now