简体   繁体   中英

How to convert System.Byte[] to Image

Hi i have the following code in which the data variable have been filled with array of bytes from databse now when i want to convert these stored bytes in the db into the image and want to show them in the picture box it gives an error at

pictureBox1.Image = Image.FromStream(ms);

And the error is Parameter is not valid. What should i do following is my code:

byte[] data = (byte[])ds.Tables[0].Rows[i][7];
MemoryStream ms = new MemoryStream(data);
ms.Write(data, 0, data.Length);
ms.Position = 0;
//Image img= Image.FromStream(ms);

pictureBox1.Image = Image.FromStream(ms);

your code looks fine ; may be Image that you are saving is not in correct format. this may be also helpful: http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx

Besides, try to set CanWrite property to True:

MemoryStream ms = new MemoryStream(data,true);

EDIT: As you have asked : the data type should be Image and i would suggest to use PNG format always rather than GIF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM