question

NazHim-9882 avatar image
0 Votes"
NazHim-9882 asked NazHim-9882 commented

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

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

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered NazHim-9882 commented

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);
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


hi Castorix31

thank you
it's working very well
thanks again

NazHim

0 Votes 0 ·