0

目前我正在使用 ASP.NET 和 C# 将图像存储到 MySQL(使用 blob 数据类型)。我已成功将其存储到数据库中,但现在的问题是,如何将其检索byte[]为图像格式?

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

1 回答 1

4

试试这个功能,

public Image byteArrayToImage(byte[] byteArrayIn)
{
     Image returnImage = null;
     using (MemoryStream ms = new MemoryStream(byteArrayIn))
     {
         returnImage = Image.FromStream(ms);
     }
     return returnImage;
}

欲了解更多信息,请访问此链接

于 2012-10-09T02:46:15.457 回答