问题
currently i am working with ASP.NET and C# to store image into MySQL (using blob datatype). I'm storing it successfully into database, but now problem is, How can I retrieve that byte[]
to image format ?
FUNCTION: code to convert byte[] to image
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); --> here gives me error as `parameter is not valid`
return returnImage;
}
retieved as datatable...
if (dt1.Rows.Count > 0)
{
byteArrayToImage((byte[]) dt1.Rows[0]["PortfolioSlideImages"]);
//MemoryStream ms = new MemoryStream((byte[])dt1.Rows[0]["PortfolioSlideImages"]);
//Image returnImage = Image.FromStream(ms);
//return returnImage;
}
回答1:
Try this function,
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
for more info, visit this link
来源:https://stackoverflow.com/questions/12792175/converting-byte-to-image-and-image-to-byte-using-c-sharp