By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,160 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,160 IT Pros & Developers. It's quick & easy.

Byte[] to Image

P: n/a
Hi,

I want to convert a byte[] to a Image. I read in a newsgroup that the
best way is to use a MemoryStream. I tried it, but I get always a
Exception while creating the Image(An unhandled exception of type
'System.ArgumentException' occurred in system.drawing.dll. Additional
information: Ungültiger Parameter verwendet). Here is my code:

Image newImage;
using (MemoryStream ms = new MemoryStream(length))
{
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
newImage = Image.FromStream(ms);
}
}

Thanks for your help !
Arno
Apr 2 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
You have to set stream's position to start before passing it to the Image
constructor.
ms.Position = 0;

"Arno" <ki******@gmx.de> wrote in message news:e0**********@online.de...
Hi,

I want to convert a byte[] to a Image. I read in a newsgroup that the best
way is to use a MemoryStream. I tried it, but I get always a Exception
while creating the Image(An unhandled exception of type
'System.ArgumentException' occurred in system.drawing.dll. Additional
information: Ungültiger Parameter verwendet). Here is my code:

Image newImage;
using (MemoryStream ms = new MemoryStream(length))
{
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
newImage = Image.FromStream(ms);
}
}

Thanks for your help !
Arno

Apr 2 '06 #2

P: n/a
Lebesgue <no****@spam.jp> wrote:
You have to set stream's position to start before passing it to the Image
constructor.
ms.Position = 0;


You also have to *not* use a using statement in this case - as
otherwise the stream will be closed automatically. Image.FromStream
requires the stream to stay open until *it* wants to close it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 2 '06 #3

P: n/a
Hi,

thanks for your help so far ... I made the changes you (both) told me,
but I still get the same Exception (System.ArgumentException) ...
My code now looks like this:

Image newImage;
MemoryStream ms = new MemoryStream(length);
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
ms.Position = 0;
newImage = Image.FromStream(ms);

thanks,
Arno

Lebesgue <no****@spam.jp> wrote:
You have to set stream's position to start before passing it to the Image
constructor.
ms.Position = 0;

You also have to *not* use a using statement in this case - as
otherwise the stream will be closed automatically. Image.FromStream
requires the stream to stay open until *it* wants to close it.

Apr 2 '06 #4

P: n/a
Arno <ki******@gmx.de> wrote:
thanks for your help so far ... I made the changes you (both) told me,
but I still get the same Exception (System.ArgumentException) ...
My code now looks like this:

Image newImage;
MemoryStream ms = new MemoryStream(length);
byte[] bArray = new byte[length];
for(int k=0; k<length; k++)
{
bArray[k] = 10;
}
ms.Write(bArray,0,length);
ms.Position = 0;
newImage = Image.FromStream(ms);


Well, that's not a valid image at the moment. Image.FromStream tries to
load the stream as if it's an image file (jpg, gif etc) - a stream of
bytes all of which are 10 isn't going to be a valid jpg.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 2 '06 #5

P: n/a
Maybe this will help:
public static byte[]
ConvertImageToByteArray(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage)
{
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;
}

public static Image ConvertByteArrayToImage(byte[] byteArray)
{
if (byteArray != null)
{
MemoryStream ms = new MemoryStream(byteArray, 0,
byteArray.Length);
ms.Write(byteArray, 0, byteArray.Length);
return Image.FromStream(ms, true);
}
return null;
}

Apr 2 '06 #6

P: n/a
catch (Exception) { throw; }

Why would one catch exception just to throw it away?

"Vince" <da****@o2.pl> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Maybe this will help:
public static byte[]
ConvertImageToByteArray(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage)
{
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;
}

public static Image ConvertByteArrayToImage(byte[] byteArray)
{
if (byteArray != null)
{
MemoryStream ms = new MemoryStream(byteArray, 0,
byteArray.Length);
ms.Write(byteArray, 0, byteArray.Length);
return Image.FromStream(ms, true);
}
return null;
}

Apr 2 '06 #7

P: n/a
You catch exception to throw it away in your own words,
or you might do this, to handle it in some where else, in your own style ..
Actually there are many reasons to do so, and if you don't like it, you can
ignore it...

I hope this helps
Khaled Hussein

"Lebesgue" <no****@spam.jp> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
catch (Exception) { throw; }

Why would one catch exception just to throw it away?

"Vince" <da****@o2.pl> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Maybe this will help:
public static byte[]
ConvertImageToByteArray(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage)
{
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;
}

public static Image ConvertByteArrayToImage(byte[] byteArray)
{
if (byteArray != null)
{
MemoryStream ms = new MemoryStream(byteArray, 0,
byteArray.Length);
ms.Write(byteArray, 0, byteArray.Length);
return Image.FromStream(ms, true);
}
return null;
}


Apr 2 '06 #8

P: n/a
Lebesgue <no****@spam.jp> wrote:
catch (Exception) { throw; }

Why would one catch exception just to throw it away?


The only reason I've seen for this that made any sense was to be able
to hit breakpoints in the debugger and examine the exception. I'd
usually try to get rid of it afterwards though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 2 '06 #9

P: n/a
I understand the reasons for catching, processing and throwing away
exceptions, don't worry. What I don't understand is catching exception JUST
to throw it away (notice there was just throw, not throw new
MyException(ex);

"Khaled Hussein" <kh************@gmail.com> wrote in message
news:um**************@tk2msftngp13.phx.gbl...
You catch exception to throw it away in your own words,
or you might do this, to handle it in some where else, in your own style
.. Actually there are many reasons to do so, and if you don't like it, you
can ignore it...

I hope this helps
Khaled Hussein

"Lebesgue" <no****@spam.jp> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
catch (Exception) { throw; }

Why would one catch exception just to throw it away?

"Vince" <da****@o2.pl> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Maybe this will help:
public static byte[]
ConvertImageToByteArray(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage)
{
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;
}

public static Image ConvertByteArrayToImage(byte[] byteArray)
{
if (byteArray != null)
{
MemoryStream ms = new MemoryStream(byteArray, 0,
byteArray.Length);
ms.Write(byteArray, 0, byteArray.Length);
return Image.FromStream(ms, true);
}
return null;
}



Apr 3 '06 #10

P: n/a
In Compact Framework the method "Image.FromStream" is not supported!
What can I do?
*** Sent via Developersdex http://www.developersdex.com ***
Jan 11 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.