1

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:

jdaval
  • 610
  • 5
  • 10
user3650075
  • 19
  • 2
  • 9

1 Answers1

-1

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!

jdaval
  • 610
  • 5
  • 10