2012-01-21 20 views
1

例如,這裏是示例JPEG,我不能保存(!),但可以使用標準的dotnet類讀取(例如查找寬度和高度)。原始文件:JPEG操作和dotnet - GDI +例外

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

在Windows圖像編輯器保存同一圖像後,所有的偉大工程: http://dl.dropbox.com/u/5079317/184809_1_resaved.jpg

我很久以前就注意到了這個錯誤,但它不是主要問題。但在目前的項目中,我有成千上萬的這樣的圖像,我真的需要某種解決方案。

可以使用哪些第三方庫?

這裏是我正在讀:

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) 
    ... 
} 

回答

0

大小調整原始圖像大小相同的解決問題:

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

我不知道你正在使用的庫SaveImage,但如果你僅僅使用.NET,調用下面的保存方法(與空返回類型),並根據新文件中的System.Drawing.Image對象返回所需的任何對象。

source.Save(@"C:\{path}\184809_1_resaved.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 

而不必更多的細節,這是我能提供的最好的,因爲我不知道是什麼的鏡像文件類的實現看起來像做。 ImageFile是你當前的返回類型,但我只是改變了類型以使其工作。

public System.IO.Stream SaveImage(HttpPostedFileBase file, string fileNameWithPath, bool splitImage, out string errorMessage) 
{ 
    try 
    { 
     using (var stream = file.InputStream) 
     { 
      using (System.Drawing.Image source = System.Drawing.Image.FromStream(stream)) 
      { 
       source.Save(@"C:\resaved.jpg", ImageFormat.Jpeg); 
       source.Save(stream, ImageFormat.Jpeg); 
       stream.Position = 0; 
       errorMessage = string.Empty; 
       return stream; 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     errorMessage = e.Message.ToString(); 
    } 
    return null; 
} 
0

Iirc有些格式需要查找,這在所有流上都不支持。您可以嘗試緩存在內存流:

using (var input = file.InputStream) 
using (var buffer = new MemoryStream()) 
{ 
    input.CopyTo(buffer); 
    buffer.Position = 0; // rewind 
    using (Image source = Image.FromStream(buffer)) 
    { ... Etc as before ... } 
}