简体   繁体   English

如何调整图像大小而不会降低质量

[英]How resize image without losing quality

I have a big image in good quality (for my needs), i need resize to small size (30 x 30px), I resize it with graphic.DrawImage. 我有一个高质量的大图像(根据我的需要),我需要调整到小尺寸(30 x 30px),我用graphic.DrawImage调整它的大小。 But when i resize it become blurred and little lighter. 但是当我调整大小时,它会变得模糊不清。 also I have try CompositingQuality and InterpolationMode, but it all was bad. 我也尝试过CompositingQuality和InterpolationMode,但这一切都很糟糕。

Example, that quality i'm trying get. 例如,我正在尝试的质量。

My result 我的结果

Edited Image of icon i draw myself, maybe it will be better draw it small without resizing? 编辑图标的图像我画自己,也许它会更好地绘制它而不调整大小?

Edit2 EDIT2

Resizeing code: 调整代码大小:

                Bitmap tbmp;
                //drawing all my features in tbmp with graphics
                bmp = new Bitmap(width + 5, height + 5);
                bmp.MakeTransparent(Color.Black);
                using (var gg = Graphics.FromImage(bmp))
                {
                    gg.CompositingQuality = CompositingQuality.HighQuality;
                  //  gg.SmoothingMode = SmoothingMode.HighQuality;
                    gg.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    gg.DrawImage(tbmp, new Rectangle(0, 0, width, height), new Rectangle(GXMin, GYMin, GXMax + 20, GYMax + 20), GraphicsUnit.Pixel);
                    gg.Dispose();
                }

I use this method as a way to get a thumbnail image (of any size) from an original (of any size). 我使用此方法作为从原始(任何大小)获取缩略图(任何大小)的方法。 Note that there are inherent issues when you ask for a size ratio that varies greatly from that of the original. 请注意,当您要求的尺寸比率与原始尺寸比率大不相同时,存在固有问题。 Best to ask for sizes that are in scale to one another: 最好问一下彼此规模的尺寸:

public static Image GetThumbnailImage(Image OriginalImage, Size ThumbSize)
{
    Int32 thWidth = ThumbSize.Width;
    Int32 thHeight = ThumbSize.Height;
    Image i = OriginalImage;
    Int32 w = i.Width;
    Int32 h = i.Height;
    Int32 th = thWidth;
    Int32 tw = thWidth;
    if (h > w)
    {
        Double ratio = (Double)w / (Double)h;
        th = thHeight < h ? thHeight : h;
        tw = thWidth < w ? (Int32)(ratio * thWidth) : w;
    }
    else
    {
        Double ratio = (Double)h / (Double)w;
        th = thHeight < h ? (Int32)(ratio * thHeight) : h;
        tw = thWidth < w ? thWidth : w;
    }
    Bitmap target = new Bitmap(tw, th);
    Graphics g = Graphics.FromImage(target);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.High;
    Rectangle rect = new Rectangle(0, 0, tw, th);
    g.DrawImage(i, rect, 0, 0, w, h, GraphicsUnit.Pixel);
    return (Image)target;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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