C # Creating thumbnails (low quality and large size issue)

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 put 700pxit so that you can better understand the problem. Here is the original and resized one.

Any good recommendation?

Thanks,
Ile

+3
source share
5 answers

You should find my answer to this question useful. It includes a sample to scale high quality images in C #.

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

Here is the corresponding 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; 
    } 
+5
source

, ..., (, ;))

MSDN...

thumbnail image, . , .

...

  • Windows: JPEG (: 830: 3234)
  • : JPEG
  • : 112
  • : 84
+2

.

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);
+1
source

Are third-party apps allowed? If so, you might want to check out ImageMagick to control thumbnail creation. There is a .NET shell.

http://imagemagick.codeplex.com/

0
source

I wrote a free .dll that makes this easy. This is here if you want to see the documentation .... Git Repository and Tutorial

0
source

Source: https://habr.com/ru/post/1745990/


All Articles