473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Byte[] to Image

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
10 47857
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
2
by: Li Zhang | last post by:
I have a problem with reading byte from a memory stream. Basically I want to resize a image and then store them to sql server. Here is part of the codes. every time I tried to read byte from...
6
by: Luis Arvayo | last post by:
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...
2
by: Knoxy | last post by:
Hi there, Hope someone can help. I've had a look around on the web and found some useful info but not getting this to work... seen lots of different variations PROBLEM: ======== I have a...
0
by: sebascomeau | last post by:
Hi everyone, Hello my name is Sebastien and I need some help. He have one week past and I search to do one thing but I can't. If someone help me, I'll be so happy :). My problem is I want to...
5
by: paul | last post by:
Hi all, Could some kind soul peruse the following code and see if there is anything wrong with it? Its producing output, but its only occupying the first third of the output array; to give an...
4
by: Rainer Queck | last post by:
Hi NG, is there a way to copy a buffer pointed to by a IntPtr directly into a two dimensional byte-array? I tried this, what obviously doesn't work: byte image = new byte; IntPtr p =...
1
by: Stephen.Schoenberger | last post by:
I am working with some images as byte arrays and am not sure nor can I figure out if as I am reading the image and saving smaller sub- images I am reading across and down the image or down and back...
0
by: velmani | last post by:
hi , i have problem with Retrieving image from DB i have a table with image datatype i store a file by using code as follows -------------------------------------------------- string strType; ...
8
by: anandmms | last post by:
Hello friend, my problem is to save more than one fingerprint images in one byte array and then store in database(oracle). i dont know how to solve it. But previously i had did image saving...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.