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

NazHim 201 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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,240 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,721 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