繁体   English   中英

将dataimage字符串转换为image时,GDI +中发生一般错误

[英]A generic error occurred in GDI+ while converting dataimage string to image

我正在尝试将图像日期保存到物理文件

以下是我从jpeg图像(通过某些浏览器响应)获得的图像数据:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/.....blah blah .....//2Q==

下面是我用来将图像数据字符串保存到Image的代码

Image image = LoadImage(dataURL);
image.Save(saveLocation);
image.Dispose();

public Image LoadImage(string imageString)
{
    imageString = imageString.Substring(imageString.IndexOf(',') + 1);
    byte[] bytes = Convert.FromBase64String(imageString);

    Image image;
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        image = Image.FromStream(ms);
    }

    return image;
}

image.Save(saveLocation);处获得以下异常: 线

GDI +中发生一般错误。

当具有相同代码的png图像但具有jpeg图像的图像源时,无论jpeg的大小如何,我每次都遇到该异常时,我没有任何问题。

我能够用相同的代码将png图像保存在相同的位置。

编辑:请注意,我只是从浏览器中获取数据图像字符串(这是通过浏览器剪贴板捕获的),即我没有从客户端获取字节。

可以转换为图像流然后保存回物理文件的字节有什么限制?

还有其他方法可以做到这一点吗?

似乎根本没有任何理由可以通过Image

假设发布的数据有效,只需将内容直接写入文件:

SaveImage(dataURL, saveLocation);

public bool SaveImage(string imageString, string location)
{
    try {
        imageString = imageString.Substring(imageString.IndexOf(',') + 1);
        byte[] bytes = Convert.FromBase64String(imageString);

        using (FileStream fs = new FileStream(location, FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Count);
        }
    }
    catch(Exception)
    {
        return false;
    }
    return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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