Convert Transparent PNG to JPG with white Background Color in C#

NazHim 206 Reputation points
2021-10-31T06:06:53.24+00:00

hi all

i am using C#. i want to Convert Transparent PNG to JPG with white Background Color
normally i am using these code.

Image img = Image.FromFile(filename);
img.Save(newFilename, System.Drawing.Imaging.ImageFormat.Jpeg);

it works fine. but background color is black.
Is there any way to make the background white instead?

best regards
NazHim

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-10-31T09:19:54.813+00:00

    For example :

                Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.White);
                    g.DrawImage(img, new Rectangle(new Point(), img.Size), new Rectangle(new Point(), img.Size), GraphicsUnit.Pixel);                    
                }
                bmp.Save(newFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.