C # mvc loading image with server resizing

I have a web application in which users can upload images. The current problem I am facing is that the uploaded images are saved in the database in the original format. This causes many performance issues when using images on a web page. I used dotTrace to profile the application, and I see significant problems when processing images from the database.

The idea I have is to resize the image when uploading it to the server. Take the following example, which I want the application to execute when the user uploads a new image;

  • User uploads image.
  • Image size resizes to 7,500 x 7,500 pixels at 72 dpi
  • Image is saved in the database
  • The original file is deleted.

The only image saved is the one mentioned above, and the web application contains technology for resizing on the fly.

I already read several topics here about SO. And most of them point me towards ImageMagick. This tool is already familiar in my company and is used in PHP projects. But are there any good and stable released C # wrappers for this tool? I already found the tools below, but they are either in the Bรฉta, Alpha Release, or not currently updated.

ImageMagick.NET

ImageMagick APP

I also found this topic on SO. The following code example is provided in this section:

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; } 

In short, I have:

  • Are there any performance benefits using the above example?
  • Is there a good and reliable C # wrapper for ImageMagick that I can use for this?

Any other helpful performance tips are welcome!

+7
source share
3 answers

We use the latter approach - I can not comment on performance, but, of course, simplifies the processing of dependencies.

However, it should be noted that the code above is probably too simple if your users can upload images in all kinds of formats. The base library (GDI +) has problems with a large number of color formats, but it also depends on the OS version. Here is the core of the code used:

  // 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); } } } 
+3
source

I never used ImageMagic, but I used the GDI + image resizing features, including on a site that generates and resizes 100,000 images per day without performance issues.

I would say that using GDI + methods is fine. Do not worry about wrapping an external tool or framework.

+2
source

I used ImageGen on Umbraco sites. (Of course, it is not tied to Umbraco, itโ€™s good for any ASP.NET application, it so happened that some of the Umbraco packages I used required it.) It is easy to use, and you could get rid of the free version .. .

+1
source

All Articles