简体   繁体   English

图像到字节数组失败“GDI+ 中发生一般错误。”

[英]Image to byte array fails “A generic error occurred in GDI+.”

let's assume I let the user choose an image from the computer.假设我让用户从计算机中选择图像。 I load the file to a picture box.我将文件加载到图片框。 here is the conversion method:这是转换方法:

    public static Image LoadImageFromFile(string fileName)
    {
        Image result = null;
        if (!File.Exists(fileName))
            return result;
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        try
        {
            using (Image image = Image.FromStream(fs))
            {
                ImageManipulation.RotateImageByExifOrientationData(image);
                result =(Image) image.Clone();
                image.Dispose();
            }
        }
        finally
        {
            fs.Close();
            
        }

        return result;
    }

then, when the user clicks on the Save button, I convert the image into a byte array and save it into the database.然后,当用户单击“保存”按钮时,我将图像转换为字节数组并将其保存到数据库中。 here is the conversion code:这是转换代码:

    public static byte[] ImageToByteArray(Image image)
    {
        if (image == null)
            return null;
        using (var ms = new MemoryStream())
        {
            ImageFormat imageFormat = image.RawFormat;
            ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == imageFormat.Guid);
            var encoderParameters = new EncoderParameters(1);
            encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
            if (codec != null)
                image.Save(ms, codec, encoderParameters);
            else
                image.Save(ms, ImageFormat.Jpeg);
            return ms.ToArray();
        }
    }

but the problem: I have a jpg file on the disk.但问题是:我在磁盘上有一个 jpg 文件。 I can load it into my picture box without any problem.我可以毫无问题地将它加载到我的图片框中。 the picture is perfectly visible in it.图片在其中完全可见。 but when I save it, the code gives me "A generic error occurred in GDI+."但是当我保存它时,代码给了我“GDI+ 中发生一般错误”。 Error at 'image.Save(ms, codec,encoderParameters)' line. 'image.Save(ms, codec,encoderParameters)' 行出错。

more odd incident: I don't get this error all the time.更奇怪的事件:我不会一直收到这个错误。 it is happening with specific images.它发生在特定图像上。 for example, I downloaded an image from the internet and crop it in "Paint" and saved it as jpg.例如,我从互联网上下载了一张图片,然后在“Paint”中裁剪并保存为 jpg。 error happened.发生错误。 open it in Paint again and save it as png.再次在 Paint 中打开它并将其另存为 png。 no error!!!!没有错误!!!! that is why I am really confused .这就是为什么我真的很困惑。 and Yes I already have tried to don't dispose the image.是的,我已经尝试不处理图像。 not helping没有帮助

I know it might be a stupid question but I am desperately stuck here :)我知道这可能是一个愚蠢的问题,但我拼命地被困在这里:)

Thank you in Advanced感谢您在高级

I didn't find out why my code is not working but I just substitute my loading method with the code below.我没有找到为什么我的代码不起作用,但我只是用下面的代码替换了我的加载方法。 it is releasing the file and at the same time works as it should.它正在释放文件,同时正常工作。

        public static Image LoadImageFromFile(string fileName)
    {
        using (Bitmap bmb = new Bitmap(fileName))
        {
            MemoryStream m = new MemoryStream();
            bmb.Save(m, ImageFormat.Bmp);
            return Image.FromStream(m);
        }
    }

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

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