views:

342

answers:

4
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

+1  A: 

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);
munissor
this works nicely... thanks munissor :)
ile
A: 

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/

bryanjonker
I'm allowed to, but I think that built-in solution should do the work (I don't see many complaining :)). Thank you for advise!
ile
+4  A: 

Hi ile,

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; 
    } 
DoctaJonez
Ile... One of the important parts of the Docta's answer is :graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;In your original code every pixel is just moved and made larger without regard for the neighboring pixels....Interpolation, Smoothing and Compositing are key.
Rusty
How to save thumbnail as a file? I tried result.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg"); but I get blank image? Thanks!
ile
ile, take a look at the answer I linked to, there's a function called SaveJpeg that you will find useful.
DoctaJonez
I sent you comment there... thanks :)
ile
You'll need to copy the whole class, it includes the GetEncoderInfo method which you asked about. It should read GetEncoderInfo and not getEncoderInfo. I fixed the typo in the other answer.
DoctaJonez
It works smoothly now :) Thanks!
ile
You're welcome :-) I'm glad I could help, I remember how happy I was when I got this working. Have fun manipulating images! :-)
DoctaJonez
+2  A: 

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
cyberzed
Thanks for the information, I always wondered why this was the case!
DoctaJonez