2011-01-19 47 views
7

我有一個web應用程序,用戶可以上傳圖像。目前我遇到的問題是上傳的圖像以原始格式保存到數據庫中。當在網頁上使用圖像時,這會導致很多性能問題。我使用dotTrace來分析應用程序,並且在從數據庫處理圖像時發現了重大問題。C#mvc圖像上傳調整大小服務器端

我的想法是在圖像上傳到服務器時調整圖像大小。以下面的例子,我想讓應用程序在用戶上傳新圖片時執行;

  1. 用戶上傳的圖像
  2. 圖像被調整爲7.500 X 7.500在像素的尺寸在72 DPI
  3. 圖像被保存到
  4. 原始文件被設置
  5. 數據庫

唯一存儲的圖像是上面提到的圖像,並且web應用程序包含用於動態調整此大小的技術。

我已經在SO上閱讀過幾個主題。他們中的大多數將我指向ImageMagick的方向。這個工具在我的公司已經很熟悉了,並在PHP項目中使用。但是這個工具有沒有好的和穩定的C#包裝器?我已經找到了下面的工具,但它們要麼是Béta版本,Alpha版本,要麼目前沒有更新。

ImageMagick.NET

ImageMagick APP

我還發現SO this話題。在這個主題中提供了下面的代碼示例;

private static Image CreateReducedImage(Image imgOrig, Size newSize) 
{ 
    var newBm = new Bitmap(newSize.Width, newSize.Height); 
    using (var newGrapics = Graphics.FromImage(newBm)) 
    { 
     newGrapics.CompositingQuality = CompositingQuality.HighSpeed; 
     newGrapics.SmoothingMode = SmoothingMode.HighSpeed; 
     newGrapics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     newGrapics.DrawImage(imgOrig, new Rectangle(0, 0, newSize.Width, newSize.Height)); 
    } 

    return newBm; 
} 

簡而言之,我有問題;

  • 使用上面的例子在性能方面有什麼優勢嗎?
  • 是否有一個好的和可靠的ImageMagick C#包裝我可以用來做到這一點?

歡迎與演出有關的任何其他好的提示!

+1

http://imageresizing.net是要走的路。它經過測試,可靠,工作信任度低。它可以輕鬆處理各種格式,並可以在單行代碼中調用。非常靈活和有能力的圖書館。 – 2011-07-16 17:26:28

回答

3

我們使用後一種方法 - 我無法評論性能,但它肯定會使處理依賴性變得更簡單。

但是,有一點需要注意的是,如果您的用戶能夠以各種格式上傳圖片,上面的代碼可能太簡單了。底層庫(GDI +)具有很多顏色格式的問題,但它也取決於操作系統版本。這裏是我們使用的代碼的核心:

// GDI+ has problems with lots of image formats, and it also chokes on unknown ones (like CMYK). 
// Therefore, we're going to take a whitelist approach. 
// see http://bmpinroad.blogspot.com/2006/04/file-formats-pixel-formats.html 
// also see http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c626a478-e5ef-4a5e-9a73-599b3b7a6ecc 
PixelFormat format = originalImage.PixelFormat; 

if (format == PixelFormat.Format16bppArgb1555 || 
    format == PixelFormat.Format64bppArgb) 
{ 
    // try to preserve transparency 
    format = PixelFormat.Format32bppArgb; 
} 
else if (format == PixelFormat.Format64bppPArgb) 
{ 
    // try to preserve pre-multiplied transparency 
    format = PixelFormat.Format32bppPArgb; 
} 
else if (format != PixelFormat.Format24bppRgb && format != PixelFormat.Format32bppRgb) 
{ 
    format = PixelFormat.Format24bppRgb; 
} 


// GIF saving is probably still an issue. If we ever need to tackle it, see the following: 
// http://support.microsoft.com/kb/319061 
// http://www.bobpowell.net/giftransparency.htm 
// http://support.microsoft.com/kb/318343 


using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, format)) 
{ 
    using (Graphics Canvas = Graphics.FromImage(newImage)) 
    { 
     using (ImageAttributes attr = new ImageAttributes()) 
     { 
      attr.SetWrapMode(WrapMode.TileFlipXY); 

      Canvas.SmoothingMode = SmoothingMode.AntiAlias; 
      Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; 
      Canvas.DrawImage(originalImage, new Rectangle(new Point(0, 0), newSize), srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, GraphicsUnit.Pixel, attr); 
      newImage.Save(outputImageStream, originalImage.RawFormat); 
     } 
    } 
} 
2

我從來沒有使用ImageMagic,但我已經使用了GDI +圖像調整大小功能,包括在每天生成並調整大小超過100,000張圖像的網站上,而不存在性能問題。

我會說使用GDI +方法就好了。不要擔心包裝外部工具或框架。

+0

當前正試圖使用​​GDI +實現一個很好的解決方案。將讓你知道結果 – Rob 2011-01-20 15:18:42

+0

GDI方法就好,只要你[使用(){}子句處理所有內容並避免25個以上的錯誤](http://nathanaeljones.com/163/20-image-resizing -pitfalls /)。 – 2011-10-24 12:15:18

1

我在Umbraco網站使用了ImageGen。 (它當然不綁定到Umbraco,它對任何ASP.NET應用程序都很有用,它恰好發生了一些我使用的Umbraco軟件包需要它的地方)。它使用起來很簡單,而且您可能可以免費使用版本...