1
votes

How to resize images without converting it to Bitmap format? My .jpg picture quality is significantly loses color quality if i convert it to .bmp. Can I use a third-party library or is there another solution?

I've tried use Paint for save this file to 16, 24 and 32 .bmp file and it loses color quality.

Code:

Image img = Image.FromFile(@"c:\1.jpg");
Bitmap bmp = new Bitmap(img);
bmp.Save(@"c:\1_save.jpg");

Image:

1
Perhaps reading up on lossless image processing: stackoverflow.com/questions/12298515/…Zach M.
It's about the jpg format. But in my example, we talking about .bmp.user3650075
This link is probably what you are after: stackoverflow.com/questions/1484759/…Ulric
Look good, thank you!user3650075

1 Answers

-1
votes

Try This.

public static void ScaleAndSave()
    {
        using (var image = Image.FromFile(@"c:\logo.png"))
        using (var newImage = ScaleImage(image, 300, 400))
        {
            newImage.Save(@"c:\ScaledAndSaved.png", ImageFormat.Png);
        }
    }

public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);

    var newImage = new Bitmap(newWidth, newHeight);

    using (var graphics = Graphics.FromImage(newImage))
        graphics.DrawImage(image, 0, 0, newWidth, newHeight);

    return newImage;
}

Hope this helps!