2

Possible Duplicate:
How to Resize Image and save in folder?

I would like to resize and save as images in a given folder. But i want to do it with maximum possible quality. I am willing to use third party software.

Like paint.net or photoshop.

So what is the best possible way of achieving this ?

Thank you.

Community
  • 1
  • 1
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • 1
    What formats are you looking to resize? Have you looked into C#'s Image classes? – justderb Dec 05 '12 at 20:08
  • 2
    What have you already tried? – Frank van Puffelen Dec 05 '12 at 20:23
  • I know only classical image processing. So don't have any knowledge of resizing. But the main issue is, i want to quality of photoshop or paint.net when resizing. I am looking to resize png images. So maximum quality is my aim. Manually i am able to batch resize at photoshop but i really need C# application :) – Furkan Gözükara Dec 05 '12 at 20:25
  • @Frank van Puffelen not at all. the maximum quality is something i am looking for not just resizing :) – Furkan Gözükara Dec 05 '12 at 20:26
  • The code in the question and answer I linked to looks like a pretty good starting point. Start with that and apply it to your situation (such as changing the image format to png). – Frank van Puffelen Dec 05 '12 at 20:32
  • I can't risk lower quality image resizing as i will resize my game main components. But if it was like user uploaded avatar, that would definitely work great :) – Furkan Gözükara Dec 05 '12 at 20:43

3 Answers3

3

Disclaimer: The following link/code is from my blog:

You should take a look at this post on my site http://samuelhaddad.com/2011/01/26/net-high-quality-thumbnail-generation/

The code from the post looks something like:

//Image Resize Helper Method
private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
{
    using (Image originalImage = Image.FromFile(filename))
    {
        //Caluate new Size
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = (int)Math.Round(newWidth / aspectRatio);
        }
        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = (int)Math.Round(newHeight * aspectRatio);
        }
        Bitmap newImage = new Bitmap(newWidth, newHeight);
        using (Graphics g = Graphics.FromImage(newImage))
        {
            //--Quality Settings Adjust to fit your application
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
            return newImage;
        }
    }
}

You can wrap that in a for loop for batch processing.

Sam Plus Plus
  • 4,381
  • 2
  • 21
  • 43
  • thanks i will try and compare with photoshop & paint.net – Furkan Gözükara Dec 05 '12 at 21:03
  • I should add that you can save back to differnt file formats by using the bitmap.save method. Which allows you to specify image formats and differnt encoders. http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx – Sam Plus Plus Dec 05 '12 at 21:03
1

Edit: Actually for much better presentation and description of the type of information I talk about below see this library and this article.

But I would also imagine most images libraries would have lanczos available. I have used FreeImage, which seems to have a mostly-decent C# wrapper.


Quality is largely about the interpolation algorithm used, and the ratio of the resizing. In Paint.net and Photoshop they have a dropdown to select them. Each algorithm will vary in what it does best (downsizing, upsizing, resizing by a integer ratio...) and each will have different speeds. Also Paint.net has the "Best Quality" option which will tell you what it decides to use (although I'm not sure what super sampling refers to...), doing manually do that with a few of your images and target size to see what it uses might help you choose a quicker algorithm.

@SamPlusPlus's answer uses HighQualityBilinear which IIRC is great for resizing and adding a small amount of blur (so sharp lines would be anti-aliased by the algorithm) Where Bicubic will retain more sharpness of lines. So out of these two, bilinear is generally better for upsizing, and bicubic is generally better for downsizing.

I believe the Lanczos algorithm is one of the best all-around algorithms (does very good at any amount of upsizing or downsizing) but is also one of the slower ones. I'd highly suggest it if you were reducing images by say 7% (the ratio 100:93 is difficult to reduce).

If you're doing 50%, 25%, 200%, etc all the algorithms will be very similar results, with speed being the only difference.

I would suggest looking for a library that has Lanczos resizing algorithm if the size of either the input or the output files is going to vary much, due to it being very good no matter the type of resizing, and because you did not mention speed being important, only quality. I have not used such a library in C#, so I do not have any direct recommendations.

Community
  • 1
  • 1
Thymine
  • 8,775
  • 2
  • 35
  • 47
  • thanks for answer. i will do only down sizing. i am looking best quality. so the time speed are not important factors here. – Furkan Gözükara Dec 05 '12 at 22:58
  • do you know any C# application that does Lanczos ? – Furkan Gözükara Dec 05 '12 at 22:59
  • This seems to be the main search result I can find: http://www.codeproject.com/Articles/11143/Image-Resizing-outperform-GDI Also this has some good information similar to what I stated (but it is more accurate than I was) http://www.codinghorror.com/blog/2007/07/better-image-resizing.html – Thymine Dec 05 '12 at 23:08
  • See edit for a couple more libraries. Also you could look at either calling imagemagick's convert.exe via command line, or use an API for it: http://www.imagemagick.org/script/api.php There is tons of information out there about imagemagick, and I have used it for a long time via commandline – Thymine Dec 05 '12 at 23:33
  • thanks i may use imagemagick . how is that software quality ? equal to paint.net or photoshop when resizing (lower dimension) – Furkan Gözükara Dec 06 '12 at 02:07
  • imagemagick with the right settings is going to be as good as you can get for free at very least, but sometimes finding the right settings can be far more difficult than using Photoshop... – Thymine Dec 06 '12 at 04:59
-1

You should look at color quantization. I've implemented a library for this and it works very well. Do a search and you should find quite a bit. I might be able to post some code later today.

Color Image Quantization in .NET

A Simple Palette Quantizer in C#

http://msdn.microsoft.com/en-us/library/aa479306.aspx

Community
  • 1
  • 1
Random
  • 1,896
  • 3
  • 21
  • 33