Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I use Image to convert image from jpg -> png it will cropped in c#

In c#, I use Image to convert image from jpg -> png it will cropped. Even I set resolution, still the same

Here is the code:

Image image = Image.FromFile(imagePath);
image.Save("Image.png", ImageFormat.Png);
image.Dispose();

The result (I use android studio icon as example):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I use Image to convert image from jpg -> png it will cropped

>Solution :

Use the Bitmap namespace

    static void ConvertJpgToPng(string imagePath)
    {
        using (Image image = Image.FromFile(imagePath))
        {
            // Create a new Bitmap with the same dimensions as the original image
            Bitmap bitmap = new Bitmap(image.Width, image.Height);

            // Create a Graphics object from the Bitmap
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // Draw the original image onto the new Bitmap
                g.DrawImage(image, 0, 0, image.Width, image.Height);
            }

            // Save the new Bitmap as PNG
            bitmap.Save("Image.png", ImageFormat.Png);
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading