How do I convert PNG to JPG in C#?

Asked

Viewed 690 times

8

I’m working on an old project that uses ASP Site.

I need to convert PNG image to JPG.

How to do this?

Note: I don’t want to rename the file, I want to transform the mime of image/png for image/jpeg.

  • 5

    Downvoter, any suggestions for improvement?

2 answers

8


One solution is to open the image and then save as JPG

Image png = Image.FromFile(@"C:\caminho-da-imagem.png");     
png .Save(@"C:\caminho-da-imagem.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Note that this will cause the bottomless parts of the PNG to stay the color black in the new image.

To avoid this, you will need to work on the original image.

The code below creates an instance of Bitmap with the original image dimensions, arrow the resolution according to the original image and then create an instance of Graphics with the method Graphics.FromImage.

With the instance of Graphics the whole surface is painted white (obviously you can do this with any color) using the method Graphics.Clear and then the original image is "drawn" on top of the new one using the method DrawImageUnscaled, the second and third parameter refer to the position in which the image will be drawn, as we want a completely equal image, we use the position (0, 0).

Image png = Image.FromFile(@"C:\caminho-da-imagem.png");
using (var bitmap = new Bitmap(png.Width, png.Height)) 
{
    bitmap.SetResolution(png.HorizontalResolution, png.VerticalResolution);

    using (var g = Graphics.FromImage(b)) 
    {
        g.Clear(Color.White);
        g.DrawImageUnscaled(png, 0, 0);
    }

    bitmap.Save(@"C:\caminho-nova-imagem.jpg", ImageFormat.Jpeg);
}

I did a test using this image here

Stack Overflow Logo

Click on the image (or here) to see the lack of background

The first code, generated this image

Stack Overflow Logo

The second code, generated this image

Stack Overflow Logo

  • Original source: https://stackoverflow.com/a/6513661/1845714

  • @Onosendai but this is not the original source

3

According to the MSDN: https://msdn.microsoft.com/pt-br/library/twss4wb0(v=vs.90). aspx

Upload the image:

System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\imagem-especifica.png");

Saved as a JPEG:

image1.Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Saved as a GIF:

image1.Save(@"C:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);

Saved as a PNG:

image1.Save(@"C:\test.png", System.Drawing.Imaging.ImageFormat.Png);

Extra

Assuming you are manipulating the image, for example want to resize or adjust the quality, then it is likely that you will use Bitmap, in this case use the using to prevent the file from "staying open", for example:

System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\imagem-especifica.png");

using(var bmp = new Bitmap(image1))
{
    //Faz algo aqui
}

//Salva
image1.Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.