Convert byte[] to Image

G

Guest

I have converted an image to byte[] using following code (in VS2005, CF 2.0,
WM 2003)

byte[] output;
MemoryStream ms = new MemoryStream();
inputImage.Save(ms, ImageFormat.Jpeg);
output = ms.ToArray();

Now i want to change it back but the code below doesn't work

Image newImage;
ms2 = new MemoryStream(output, 0, output.Length);
ms2.Write(output, 0, output.Length);
newImage = Image.FromStream(ms, true);

VS2005 says there is no FromStream in "Image".

Thanks.
 
P

Peter Foot [MVP]

Use the Bitmap constructor e.g.

MemoryStream ms2 = new MemoryStream(output);
newImage = new Bitmap(ms2);

Peter
 
C

cz

Value does not fall within the expected range.

I got this exception when i try to convert byte array back to image

here is my code :

pic = pub.getPicture("1111111111111")

Dim newImage As Image
Dim ms2 As MemoryStream = New MemoryStream(pic, 0, pic.Length)

ms2.Write(pic, 0, pic.Length)
p.Image = New Bitmap(ms2)

And function getPicture do just read image from sql server2005

here is that code :

public byte[] getPicture(String isbn)
{
byte[] pic;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
sqlCmd = "SELECT book_image FROM book WHERE book_isbn = '" + isbn + "'";
SqlCommand picCmd = new SqlCommand(sqlCmd,conn);
pic = (byte[])picCmd.ExecuteScalar();
conn.Close();
picCmd.Dispose();
return pic;
}

Thank all of you guys for any helps
 

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