C# Creating thumbnail (low quality and big size problem)

public void CreateThumbnail(Image img1, Photo photo, string targetDirectoryThumbs)
        {
            int newWidth = 700;
            int newHeight = 700;
            double ratio = 0;

            if (img1.Width > img1.Height)
            {
                ratio = img1.Width / (double)img1.Height;
                newHeight = (int)(newHeight / ratio);
            }
            else
            {
                ratio = img1.Height / (double)img1.Width;
                newWidth = (int)(newWidth / ratio);
            }

            Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
            bmp1.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg");

            img1.Dispose();
            bmp1.Dispose();
        }

I've put 700px so that you can have better insight of the problem. Here is original image and resized one.

Any good recommendation?

Thanks, Ile

Answers


You should find my answer to this question helpful. It includes a sample for high quality image scaling in C#.

The full sample in my other answer includes how to save the image as a jpeg.

Here's the relevant bit of code...

    /// <summary> 
    /// Resize the image to the specified width and height. 
    /// </summary> 
    /// <param name="image">The image to resize.</param> 
    /// <param name="width">The width to resize to.</param> 
    /// <param name="height">The height to resize to.</param> 
    /// <returns>The resized image.</returns> 
    public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height) 
    { 
        //a holder for the result 
        Bitmap result = new Bitmap(width, height); 

        //use a graphics object to draw the resized image into the bitmap 
        using (Graphics graphics = Graphics.FromImage(result)) 
        { 
            //set the resize quality modes to high quality 
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
            //draw the image into the target bitmap 
            graphics.DrawImage(image, 0, 0, result.Width, result.Height); 
        } 

        //return the resulting bitmap 
        return result; 
    } 

If the image contains a thumbnail it'll automatically stretch it to the desired size...which will make it look like crap (like your case ;))

Straight outta MSDN...

If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image.

In your case I just double checked the source image for it's thumbnail and got this...

  • New Windows Thumbnail : JPEG Format (Offset:830Size:3234)
  • Thumbnail Type : JPEG
  • Thumnail Width : 112
  • Thumbnail Height : 84

Try to draw the original image to another smaller image, and save the result.

Bitmap bmp1 = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(img1, 0, 0, newWidth, newHeight);
bmp1.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg", ImageFormat.Jpeg);

Are you allowed to use third party applications? If so, you may want to check out ImageMagick to manage thumbnail creation. There's a .NET wrapper.

http://imagemagick.codeplex.com/


I wrote a free .dll that does this easily. It's here if you want to see the documentation.... Git Repository & Tutorial


Need Your Help

Perl Regex to Exclude Certain TLDs for Spamassassin

regex perl uri tld spamassassin

I am not at all able to code in Perl; so, what seems like a simple thing -- writing a regex to score all URIs that are not for "com" or "net" or "org" TLDs -- is apparently beyond my skills. Could

TFS Build with PostSharp Debug | Any CPU

tfs build postsharp

I have a .Net project (framework 4.0) that references PostSharp assembly (version 2.1). The project is configured for Debug and Release with Any CPU.