Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How resize image without losing quality

Tags:

c#

graphics

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 little lighter. also I have try CompositingQuality and InterpolationMode, but it all was bad.

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

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();
                }
like image 427
BOBUK Avatar asked Aug 06 '13 15:08

BOBUK


People also ask

Can you resize pictures without ruining the quality?

Most of the time, reducing an image's size or dimensions won't affect the image's quality. Making an image to be larger than its original dimensions can be tricky. Resizing an image larger than its original dimensions can affect the quality.


1 Answers

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;
}
like image 176
DonBoitnott Avatar answered Oct 11 '22 21:10

DonBoitnott