将字节转换为图像&使用c#将图像转换为字节

时间:2012-10-09 02:44:29

标签: c# mysql image bytearray typeconverter

目前我正在使用asp.net and c#将图像存储到MySql(使用blob数据类型)。我将它成功存储到数据库中,但现在问题是how can i retrieve that byte[] to image format ?

功能: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;

    }

作为数据表... ...

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 个答案:

答案 0 :(得分:5)

尝试此功能,

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