Byte[] to Image

A

Arno

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
 
L

Lebesgue

You have to set stream's position to start before passing it to the Image
constructor.
ms.Position = 0;
 
J

Jon Skeet [C# MVP]

Lebesgue said:
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.
 
A

Arno

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
 
J

Jon Skeet [C# MVP]

Arno said:
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.
 
V

Vince

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;
}
 
L

Lebesgue

catch (Exception) { throw; }

Why would one catch exception just to throw it away?
 
K

Khaled Hussein

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 said:
catch (Exception) { throw; }

Why would one catch exception just to throw it away?

Vince said:
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;
}
 
J

Jon Skeet [C# MVP]

Lebesgue said:
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.
 
L

Lebesgue

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 said:
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 said:
catch (Exception) { throw; }

Why would one catch exception just to throw it away?

Vince said:
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;
}
 
M

Martin Pachinger

In Compact Framework the method "Image.FromStream" is not supported!
What can I do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top