简体   繁体   中英

Resize image in C# without losing quality

static Image ScaleImage(Image image, int Width, int Height)
{
        int originalWidth = image.Width;
        int originalHeight = image.Height;

        double ratioX = (double)Width / (double)originalWidth;
        double ratioY = (double)Height / (double)originalHeight;
        double ratio = Math.Min(ratioX, ratioY);

        int newHeight = (int)(originalHeight * ratio);
        int newWidth = (int)(originalWidth * ratio);

        Image scaledImage = new Bitmap(newWidth, newHeight);
        Graphics graphic = Graphics.FromImage(scaledImage);

        graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphic.SmoothingMode = SmoothingMode.HighQuality;
        graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphic.CompositingQuality = CompositingQuality.HighQuality;

        graphic.Clear(Color.Transparent);
        graphic.DrawImage(image, 0, 0, newWidth, newHeight);

        return scaledImage;
}

        Image full = new Bitmap("img.png");
        Image scaledImage = ScaleImage(full, full.Width / 2, full.Height / 2);
        Clipboard.SetImage(scaledImage); 

The problem is that, after resizing using this code, the image is kind of blurred.

I want to copy to clipboard a version of original image, but scaled(2/3 times smaller) and if I past the image somewhere and manually resize this(to a bigger resolution), I want the same quality as original one.

How can I do this?

try this one code, it's almost like yours, but i use it in my own pictures viewer and dont see any "blur".

It dont resize each side, it put picture in square with MaxImageSizeToResize side and save proportions. May be you see blur because of not proportional resize, or lost some properties.

Try it and say - is result the same or not.

public static System.Drawing.Bitmap Resize(string pathToOriginalFile, int MaxImageSizeToResize)
    {
        using (var image = System.Drawing.Image.FromFile(pathToOriginalFile))
        {
            return LocalGet(image);
        }

        System.Drawing.Bitmap LocalGet(System.Drawing.Image image)
        {
            int rW = 0;
            int rH = 0;

            double c = 0;
            c = ((double)image.Height / (double)MaxImageSizeToResize);
            rW = (int)(image.Width / c);
            rH = MaxImageSizeToResize;

            var destRect = new System.Drawing.Rectangle(0, 0, rW, rH);
            var destImage = new System.Drawing.Bitmap(rW, rH);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = System.Drawing.Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new System.Drawing.Imaging.ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel, wrapMode);
                }
            }

            return destImage;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM