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

 ̄綄美尐妖づ 提交于 2020-01-04 05:08:20

问题


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:


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



回答2:


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



回答3:


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



回答4:


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/




回答5:


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



来源:https://stackoverflow.com/questions/2857806/c-sharp-creating-thumbnail-low-quality-and-big-size-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!