简体   繁体   English

为什么我不能从图像数组创建图像

[英]Why can't I create an image from image array

I'm getting an error very similar to A generic error occurred in GDI+, JPEG Image to MemoryStream我收到的错误非常类似于GDI+, JPEG Image to MemoryStream 中发生的一般错误

However, I don't believe I'm shutting down the stream and as such, the answers don't apply但是,我不相信我正在关闭流,因此,答案不适用

Please consider请考虑

var img = (Tests.Properties.Resources.image).ToByteArray(); //img is a png;

using (var ms = new MemoryStream(img))
{
    var pic = Image.FromStream(ms);
    pic.Save(this._absolutePath, this._format); //kaboomn
}

The issue is the final line of code goes kaboom!问题是最后一行代码变成了kaboom!

"A generic error occurred in GDI+." “GDI+ 中出现一般错误。”

This is the extension method这是扩展方法

public static byte[] ToByteArray(this Bitmap image)
{
    using (var ms = new MemoryStream())
    {
        image.Save(ms, image.RawFormat);
        return ms.ToArray();
    }
}

So, although I'm using 2 streams, the first time returns the bytes and as such, I'm not using the same stream, I'm simply working with the bytes.所以,虽然我使用了 2 个流,但第一次返回字节,因此,我没有使用相同的流,我只是使用字节。

replace代替

using (var ms = new MemoryStream(img))
{
    var pic = Image.FromStream(ms);
    pic.Save(this._absolutePath, this._format);
}

to

var pic = Image.FromStream(new MemoryStream(img)));
pic.Save(this._absolutePath, this._format);

Check this Bitmap and Image constructor dependencies检查此位图和图像构造函数依赖项

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

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