2011-01-19 44 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软件包需要它的地方)。它使用起来很简单,而且您可能可以免费使用版本...