目前,我正在与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;
        }

最佳答案

试试这个功能,

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

关于c# - 使用C#将字节转换为图像并将图像转换为字节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12792175/

10-11 03:10