Add background color to PNG files in C#.

This code snippet will let you force a background color into a PNG file with C#. When working with PNG files in Visual Studio sometimes you’ll run into situations where the background of a PNG shows up as grey or even blue. This is because of the PNG files transparency and Windows can sometimes get confused as what to do for background color, especially when pasting a PNG file into a Wordpad document.

To use the code all you have to do is pass an Image object that you have created either from a file or a pictureBox control. Pass the height, width and the Color you want for the background color and the function will fill on the fly. This function has no return type because it’s modifying the image with the Graphics class which modify’s the image in memory as is. If you pass the Image from a pictureBox control you won’t have to pass the Image object back to the pictureBox control to have it update, it will be updated using the referenced Image object.

    private void addBgToPng(Image img,int xWidth, int xHeight,Color color)
        {
            Image imgOrig = (Image)img.Clone();
            Graphics g = Graphics.FromImage(img);
            Rectangle bgImg = new Rectangle(0, 0, xWidth, xHeight);
            SolidBrush bgBrush = new SolidBrush(color);
            g.FillRectangle(bgBrush, bgImg);
            g.DrawImage(imgOrig, new Point(0, 0));
            bgBrush.Dispose();
         }


Digg: DIGG ME

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Sponsor & Advertise
Tech Buzz

Computer for SALE!

Howdy everyone, Jimmy B here trying to sell my computer. Basically I have two computers at home right now. A nice machine with Windows 7, good for gaming, tinkering, Linux, programming and that sort of stuff. My other computer is just a normal Dell desktop which I plan to use as my main machine now [...]

Read More »

Zero-Day VBScript plagues Windows XP / 2000.

Apparently there is a new zero-day flaw that affects Windows XP and 2000 computers utilizing VBScript. An attacker can trick someone into visiting a website that binds the F1 key to a VBScript event which ultimately installs malicious code on your machine. Microsoft’s fix: Don’t press the F1 key when windows pop up. LOL. Ok [...]

Read More »

Has Verizon been hacked? Security certificates revoked!

Has Verizon been hacked? Google Chrome seems to think so. Just a few minutes ago I tried to log into Verizon to see why my phone isn’t making any calls and to also see why I can’t make any text messages. I’m going to have to probably assume they haven’t been hacked, but how does [...]

Read More »

My first blocked number in Google Voice.

Today I received my first piece of spam in Google Voice. At first I was really PISSED-OFF but then a feeling of serenity passed over my whole body as I noticed the “block” button. Slowly and cautiously I clicked it, making my day THAT MUCH better. Just knowing that I will no longer be getting [...]

Read More »

3500 Netflix on Linux petitions.

Currently you can not watch Netflix if you are a Linux user and all those new Ubuntu Netbook owners will not be watching Netflix anytime soon either. Watching movies online through Netflix is an awesome service, but worthless to Linux users. Netflix has chosen to only allow Windows and MAC users access to their online [...]

Read More »

Apple bans “android” from apps store.

Apples waving the ban stick around again, this time rejecting an educational iPhone app because it contained the word “Android”. The application? Flash of Genius: SAT Vocab 2.2, an iPhone app developed by Tim Novikof. The app did really well in the Android Developer Challenge that Google puts on and decided to mention that [...]

Read More »