Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image resizing efficiency in C# and .NET 3.5

Tags:

c#

.net

resize

gdi+

I have written a web service to resize user uploaded images and all works correctly from a functional point of view, but it causes CPU usage to spike every time it is used. It is running on Windows Server 2008 64 bit. I have tried compiling to 32 and 64 bit and get about the same results.

The heart of the service is this function:

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

I put a profiler on the service and it seemed to indicate the vast majority of the time is spent in the GDI+ library itself and there is not much to be gained in my code.

Questions: Am I doing something glaringly inefficient in my code here? It seems to conform to the example I have seen.

Are there gains to be had in using libraries other than GDI+? The benchmarks I have seen seem to indicate that GDI+ does well compare to other libraries but I didn't find enough of these to be confident.

Are there gains to be had by using "unsafe code" blocks?

Please let me know if I have not included enough of the code...I am happy to put as much up as requested but don't want to be obnoxious in the post.

like image 716
Matthew Nichols Avatar asked Aug 31 '09 12:08

Matthew Nichols


People also ask

Does resizing image lose quality?

Does resizing an image affect its quality? It definitely can! Typically, making an image smaller will not impact the quality, but an image can suffer quality loss when scaled beyond its original size.

Does PNG lose quality when resized?

Remember that GIF and JPEG codecs are “lossy,” meaning some resolution will be lost when scaling images. PNG files, on the other hand, will not degrade in quality. Note that the quality can be lost when resizing or re-formatting bitmap files such as GIF, JPEG, and even some PNGs.

How can I resize without losing quality?

The higher the resolution, the more pixels there are in the image, and the better the quality. If you want to resize an image without losing quality, you need to make sure that the "Resample Image" checkbox is unchecked. This checkbox tells Photoshop to change the number of pixels in the image.

What is the difference between scaling and resizing?

Resizing the image changes the dimensions of the image without applying a transformation to the existing contents. Scaling the image will stretch the existing contents to the new dimensions.


2 Answers

Image processing is usually an expensive operation. You have to remember that a 32 bit color image is expanded in memory into 4 * pixel width * pixel height before your app even starts any kind of processing. A spike is definitely to be expected especially when doing any kind of pixel processing.

That being said, the only place i could see you in being able to speed up the process or lowering the impact on your processor is to try a lower quality interpolation mode.

like image 94
Paul Sasik Avatar answered Sep 23 '22 19:09

Paul Sasik


I know that the DirectX being released with Windows 7 is said to provide 2D hardware acceleration. Whether this implies it will beat out GDI+ on this kind of operation, I don't know. MS has a pretty unflattering description of GDI here which implies it is slower than it should be, among other things.

If you really want to try to do this kind of stuff yourself, there is a great GDI Tutorial that shows it. The author makes use of both SetPixel and "unsafe blocks," in different parts of his tutorials.

As an aside, multi-threading will probably help you here, assuming your server has more than one CPU. That is, you can process more than one image at once and probably get faster results.

like image 36
Brian Avatar answered Sep 22 '22 19:09

Brian