469,609 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Converting image to byte array

Hello,

I am trying to convert a jpeg image stored in a PictureBox to a byte array
in order to later save it to a database, but I get this error : "Generic
Error in GDI+".

The source code is the following (when clicking in a button):

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //
<-- Error is here
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, (int)ms.Length);
...save the array to a database

The image inside the PictureBox was obtained from a jpeg file the following
way (when clicking other button and by using a OpenFileDialog in order to
define the source file):

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

Is there something I am doing wrong ?. In fact, sometimes the error does not
raise, but when I try to draw an image in some control, the image is not
displayed. It seems that GDI+ was kept in a corrupted state.

Does anyone have any hint ?

Thanks in advance
Luis A.
Nov 17 '05 #1
6 40943
Hi Luis,
I just ran the extact code you had and it compiled and executed perfectly,
I read an image into the byte array. Verify that the images you are trying
to load can be opended in other applications. Was there any more information
along with the exception that was thrown?

Mark.

"Luis Arvayo" wrote:
Hello,

I am trying to convert a jpeg image stored in a PictureBox to a byte array
in order to later save it to a database, but I get this error : "Generic
Error in GDI+".

The source code is the following (when clicking in a button):

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //
<-- Error is here
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, (int)ms.Length);
...save the array to a database

The image inside the PictureBox was obtained from a jpeg file the following
way (when clicking other button and by using a OpenFileDialog in order to
define the source file):

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

Is there something I am doing wrong ?. In fact, sometimes the error does not
raise, but when I try to draw an image in some control, the image is not
displayed. It seems that GDI+ was kept in a corrupted state.

Does anyone have any hint ?

Thanks in advance
Luis A.

Nov 17 '05 #2
http://www.eggheadcafe.com/PrintSear...asp?LINKID=799

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net

Earn $$$ money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp

"Luis Arvayo" <am**********@prodigy.net.mx> wrote in message
news:eA****************@TK2MSFTNGP10.phx.gbl...
Hello,

I am trying to convert a jpeg image stored in a PictureBox to a byte array
in order to later save it to a database, but I get this error : "Generic
Error in GDI+".

The source code is the following (when clicking in a button):

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //
<-- Error is here
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, (int)ms.Length);
..save the array to a database

The image inside the PictureBox was obtained from a jpeg file the
following way (when clicking other button and by using a OpenFileDialog in
order to define the source file):

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

Is there something I am doing wrong ?. In fact, sometimes the error does
not raise, but when I try to draw an image in some control, the image is
not displayed. It seems that GDI+ was kept in a corrupted state.

Does anyone have any hint ?

Thanks in advance
Luis A.

Nov 17 '05 #3
Robbe Morris [C# MVP] <in**@eggheadcafe.com> wrote:
http://www.eggheadcafe.com/PrintSear...asp?LINKID=799


I'm not keen on that code, I'm afraid. Calling Close manually rather
than with a using statement means that the stream isn't closed if an
exception is thrown - not a problem for a MemoryStream, but not a good
idea in general.

Then there's the:

try
{
// Stuff
}
catch (Exception e)
{
throw e;
}

What's the point of catching it if you're just going to throw it? All
that does is get rid of potentially useful bits of the stack trace.
Then there's using Image.FromStream - the docs say that you must keep
the stream open while the image is in use, but you close the stream
immediately. You're also giving it a stream which is positioned at the
*end* of the data rather than at the beginning - I believe there should
be:

oStream.Position = 0;

after the call to Write and before the call to Image.FromStream.

(MS naming conventions also suggest using camel casing for parameter
names, but that's just a minor nit-pick.)

Finally - I'm not sure how this helps the OP, who had equivalent code
for the relevant section, namely the call to Image.Save...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Good point on the try/catch block. I didn't pay much
attention to that when I posted the code block awhile ago.

As for the other items, that code (with correct try/catch)
and using works quite well in my production apps. I use
it largely for comitting chart images to and from cache.

I posted it just assuming the original poster might
need to perform both actions.

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net

Earn $$$ money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Robbe Morris [C# MVP] <in**@eggheadcafe.com> wrote:
http://www.eggheadcafe.com/PrintSear...asp?LINKID=799


I'm not keen on that code, I'm afraid. Calling Close manually rather
than with a using statement means that the stream isn't closed if an
exception is thrown - not a problem for a MemoryStream, but not a good
idea in general.

Then there's the:

try
{
// Stuff
}
catch (Exception e)
{
throw e;
}

What's the point of catching it if you're just going to throw it? All
that does is get rid of potentially useful bits of the stack trace.
Then there's using Image.FromStream - the docs say that you must keep
the stream open while the image is in use, but you close the stream
immediately. You're also giving it a stream which is positioned at the
*end* of the data rather than at the beginning - I believe there should
be:

oStream.Position = 0;

after the call to Write and before the call to Image.FromStream.

(MS naming conventions also suggest using camel casing for parameter
names, but that's just a minor nit-pick.)

Finally - I'm not sure how this helps the OP, who had equivalent code
for the relevant section, namely the call to Image.Save...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
Robbe Morris [C# MVP] <in**@eggheadcafe.com> wrote:
Good point on the try/catch block. I didn't pay much
attention to that when I posted the code block awhile ago.

As for the other items, that code (with correct try/catch)
and using works quite well in my production apps. I use
it largely for comitting chart images to and from cache.
That doesn't mean it's correct though. I suspect it entirely depends on
what you do with the image. Here's a test method (I've made your
methods static):

static void Main()
{
Image original = Image.FromFile ("test.jpg");
byte[] bytes = ConvertImageToByteArray(original);
Image converted = ConvertByteArrayToImage(bytes);
Image thumb = converted.GetThumbnailImage
(100, 100, null, new IntPtr(0));
}

All is fine until you ask for the thumbnail - at which point things go
wrong, with your code as it currently is. If you don't close the
stream, however, everything's fine. Even if it works for your cases,
doing something which goes against what the documentation specifically
requires seems a bad idea to me.

I'm still surprised that it works without repositioning the stream, but
maybe that's just something it does internally automatically. Bit of a
shame, in a way, as it presumably means you can't have a seekable
stream which contains some other data and *then* the image data..
I posted it just assuming the original poster might
need to perform both actions.


Unfortunately the problem was performing just the one action to start
with :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
I only use ConvertByteArrayToImage in one place and it doesn't really try
to do anything with the image afterwards. Thus, I never saw issues with
this.

That said, I hate to have tips out there that aren't accurate. It is
correct now.

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net
Earn $$$ money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Robbe Morris [C# MVP] <in**@eggheadcafe.com> wrote:
Good point on the try/catch block. I didn't pay much
attention to that when I posted the code block awhile ago.

As for the other items, that code (with correct try/catch)
and using works quite well in my production apps. I use
it largely for comitting chart images to and from cache.


That doesn't mean it's correct though. I suspect it entirely depends on
what you do with the image. Here's a test method (I've made your
methods static):

static void Main()
{
Image original = Image.FromFile ("test.jpg");
byte[] bytes = ConvertImageToByteArray(original);
Image converted = ConvertByteArrayToImage(bytes);
Image thumb = converted.GetThumbnailImage
(100, 100, null, new IntPtr(0));
}

All is fine until you ask for the thumbnail - at which point things go
wrong, with your code as it currently is. If you don't close the
stream, however, everything's fine. Even if it works for your cases,
doing something which goes against what the documentation specifically
requires seems a bad idea to me.

I'm still surprised that it works without repositioning the stream, but
maybe that's just something it does internally automatically. Bit of a
shame, in a way, as it presumably means you can't have a seekable
stream which contains some other data and *then* the image data..
I posted it just assuming the original poster might
need to perform both actions.


Unfortunately the problem was performing just the one action to start
with :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by lgbjr | last post: by
3 posts views Thread by grawsha2000 | last post: by
9 posts views Thread by Gregory.A.Book | last post: by
2 posts views Thread by Laurent Navarro | last post: by
2 posts views Thread by Bjorn Sagbakken | last post: by
10 posts views Thread by =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.