C#mvc图像上传resize服务器端

我有一个web应用程序,用户可以上传图像。 我遇到的当前问题是正在上传的图像以原始格式保存到数据库中。 当在网页上使用图像时,这会导致很多性能问题。 我使用dotTrace来分析应用程序,当从数据库处理图像时,我发现了很多问题。

我的想法是在图像上传到服务器时调整图像大小。 以下示例,我希望应用程序在用户上传新图像时执行此操作;

  1. 用户上传图像
  2. 在72 dpi时,图像的大小调整为7.500 x 7.500像素
  3. 图像正在保存到数据库中
  4. 原始文件被处置

唯一存储的图像是上面提到的图像,web应用程序包含动态resize的技术。

我已经在SO上阅读了几个主题。 而且他们中的大多数都指向了ImageMagick的方向。 这个工具在我的公司已经很熟悉,并且在PHP项目中使用。 但是这个工具有没有任何好的和稳定的C#包装器? 我已经找到了下面的工具,但他们要么在Béta发布,Alpha发布,要么当前没有更新。

ImageMagick.NET

ImageMagick APP

我也在SO上找到了这个话题。 在本主题中,提供了以下代码示例;

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#包装器可以用来做这个吗?

欢迎任何有关表演的其他好建议!

我们使用后一种方法 – 我不能评论性能,但它肯定会使处理依赖性更简单。

但是,需要注意的一点是,如果您的用户能够以各种格式上传图像,则上述代码可能过于简单。 底层库(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); } } } 

我从未使用过ImageMagic,但是我使用了GDI +图像resizefunction,包括在每天生成和调整100,000个图像并且没有性能问题的网站上。

我会说使用GDI +方法就好了。 不要担心包装外部工具或框架。

我在Umbraco网站上使用过ImageGen 。 (它肯定不是与Umbraco绑在一起,它对任何ASP.NET应用程序都有好处,它恰好发生了我正在使用的一些Umbraco软件包需要它。)它使用起来很简单,你也许可以免费使用它版…