奇怪的 GDI+行为

发布于 2024-08-07 13:02:19 字数 776 浏览 5 评论 0原文

我制定了一种根据图像质量压缩ImageSize的方法。它的代码是

public static Image CompressImage(string imagePath, long quality)
{
    Image srcImg = LoadImage(imagePath);
    //Image srcImg = Image.FromFile(imagePath);

    EncoderParameters parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);

    ImageCodecInfo encoder = GetCodecInfo("image/jpeg");

    srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
}

public static Image LoadImage(string filename)
{
    using (FileStream fs = new FileStream(filename, FileMode.Open))
    {
        return(Image.FromStream(fs));
    }
}

现在,当我按原样运行此代码时,它在保存 srcImg(func #1 中的最后一行)时给出了“通用 GDI+ 异常”,但是当我取消注释第二行并使用 Image. FromFile 一切正常。

为什么 ??

I have made a method to CompressImageSize according to Image quality. The code for it is

public static Image CompressImage(string imagePath, long quality)
{
    Image srcImg = LoadImage(imagePath);
    //Image srcImg = Image.FromFile(imagePath);

    EncoderParameters parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);

    ImageCodecInfo encoder = GetCodecInfo("image/jpeg");

    srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
}

public static Image LoadImage(string filename)
{
    using (FileStream fs = new FileStream(filename, FileMode.Open))
    {
        return(Image.FromStream(fs));
    }
}

Now, when i run this code as is it gives me a 'Generic GDI+ exception' while saving the srcImg(last line in func #1), BUT when i uncomment the 2nd line and load the image using Image.FromFile everything works fine.

Why ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

那支青花 2024-08-14 13:02:19

根据 MSDN

备注
您必须在图像的生命周期内保持流打开。

这里,您的流位于 using 块中,因此在图像的生命周期结束之前关闭。

According to MSDN:

Remarks:
You must keep the stream open for the lifetime of the Image.

Here your stream is in a using block and thus closed before the end of the lifetime of the image.

勿挽旧人 2024-08-14 13:02:19

您应该重写您的代码:

public static Image LoadImage(string filename)
{
    FileStream fs = new FileStream(filename, FileMode.Open);        
    return Image.FromStream(fs);        
}

在这种情况下,using 的构造是错误的,因为 FileStream 需要处于活动状态才能使用您的图像。

You should rewrite your code:

public static Image LoadImage(string filename)
{
    FileStream fs = new FileStream(filename, FileMode.Open);        
    return Image.FromStream(fs);        
}

Construction using is wrong in this case because FileStream needs to be alive for using your Image.

走过海棠暮 2024-08-14 13:02:19

一个疯狂的猜测...图像是 IDisposable。你是在循环中调用这个还是什么?尝试将图像本身放入 using() 块中?

A wild guess... Image is IDisposable. Are you calling this in a loop or something? Try putting your Image itself in a using() block?

心欲静而疯不止 2024-08-14 13:02:19

LoadImage的最后,包含Image的FileStream被释放。这还为时过早;文件流需要处于活动状态才能供调用 LoadImage 的方法使用。

请参阅 MSDN 上的使用

In the end of LoadImage, the FileStream containing the Image is disposed. This is too soon; the file stream needs to be alive for use by the method calling LoadImage.

See using on MSDN.

娇纵 2024-08-14 13:02:19

Image.FromFile() 存在各种问题...

Image srcImg = Image.FromFile(imagePath);

上述语句不会关闭文件流,如果您想再次访问文件或删除它,则会产生问题。我会这样写你的函数。

public static Image CompressImage(string imagePath, long quality)
{
    using(FileStream fs = File.OpenRead(imagePath)){
       Image srcImg = Image.FromStream(fs);    

       EncoderParameters parameters = new EncoderParameters(1);    
       parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);    
       ImageCodecInfo encoder = GetCodecInfo("image/jpeg");    
       srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
    }
}

这将保证我的文件将在使用范围结束时关闭。

There are various issues with Image.FromFile()...

Image srcImg = Image.FromFile(imagePath);

The above statement will not close the file stream and that will create problems if you want to access file again or delete it. I would write your function this way.

public static Image CompressImage(string imagePath, long quality)
{
    using(FileStream fs = File.OpenRead(imagePath)){
       Image srcImg = Image.FromStream(fs);    

       EncoderParameters parameters = new EncoderParameters(1);    
       parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);    
       ImageCodecInfo encoder = GetCodecInfo("image/jpeg");    
       srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
    }
}

This will guarentee that my file will be closed at the end of using scope.

流星番茄 2024-08-14 13:02:19

修复,但对我来说 Dispose 调用是 .net 框架中的一个错误...

public static Image CompressImage(string imagePath, long quality)
{
    Image srcImg = LoadImage(imagePath);
    //Image srcImg = Image.FromFile(imagePath);

    EncoderParameters parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);

    ImageCodecInfo encoder = GetCodecInfo("image/jpeg");

    srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
srcImg.Dispose();
}

Fix , but for me the Dispose call is a bug in .net framework...

public static Image CompressImage(string imagePath, long quality)
{
    Image srcImg = LoadImage(imagePath);
    //Image srcImg = Image.FromFile(imagePath);

    EncoderParameters parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);

    ImageCodecInfo encoder = GetCodecInfo("image/jpeg");

    srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
srcImg.Dispose();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文