How resize image without losing quality

后端 未结 1 1352
情书的邮戳
情书的邮戳 2020-12-11 07:24

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. But when i resize it become blurred and littl

相关标签:
1条回答
  • 2020-12-11 07:41

    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;
    }
    
    0 讨论(0)
提交回复
热议问题