JPEG操作和DOTNET - GDI +的异常 [英] JPEG manipulations and dotnet - GDI+ exception

查看:180
本文介绍了JPEG操作和DOTNET - GDI +的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这里是我无法保存(!),但可以读取样本JPEG(如发现宽度和高度),使用标准的dotnet类。原始文件:

http://dl.dropbox.com/u/5079317/184809_1_original.jpg

节能相同的图像窗口中的图像编辑器后,所有的伟大工程: http://dl.dropbox.com/u/5079317/184809_1_resaved.jpg

我很久以前就注意到了这个错误,但它不是主要问题。但在当前的项目我有成千上万这样的图像,我真的需要某种形式的解决方案。

哪个第三方库可以用?

下面是我如何阅读:

 公共镜像文件SaveImage(HttpPostedFileBase文件,字符串fileNameWithPath,布尔splitImage,出字符串的errorMessage)
{
  尝试
  {
    使用(VAR流= file.InputStream)
    {
      使用(图片来源= Image.FromStream(流))
      {
        返回SaveImage(来源fileNameWithPath,splitImage,出来的errorMessage);
        //实际上做source.Save(fileNameWithPath,ImageFormat.Jpeg);
        //异常:GDI +中发生一般性错误。
      }
    }
  }
  赶上(例外五)
  ...
}
 

解决方案

原始图像尺寸相同的大小调整可以解决的问题:

 图像IMG2 = FixedSize(来源source.Width,source.Height,真正的);
img2.Save(路径,ImageFormat.Jpeg);
 

For instance, here is sample JPEG which I cannot SAVE (!) but can read (e.g. find out width and height) using standard dotnet classes. Original file:

http://dl.dropbox.com/u/5079317/184809_1_original.jpg

After saving same image in windows image editor, all works great: http://dl.dropbox.com/u/5079317/184809_1_resaved.jpg

I noticed this bug long time ago but it wasn't major problem. But in current project I have thousands of such images and i really need some kind of solution.

Which 3rd party libraries could be used?

Here is how I am reading:

public ImageFile SaveImage(HttpPostedFileBase file, string fileNameWithPath, bool splitImage, out string errorMessage)
{
  try
  {
    using (var stream = file.InputStream)
    {
      using (Image source = Image.FromStream(stream))
      {
        return SaveImage(source, fileNameWithPath, splitImage, out errorMessage);
        // which actually do source.Save(fileNameWithPath, ImageFormat.Jpeg);
        // Exception: A generic error occurred in GDI+.
      }
    }
  }
  catch (Exception e)
  ...
}

解决方案

Resizing of original image to same size solves problem:

Image img2 = FixedSize(source, source.Width, source.Height, true);
img2.Save(path, ImageFormat.Jpeg);

这篇关于JPEG操作和DOTNET - GDI +的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆