.NET how to set the background color of a bitmap

by Cliff 3. September 2009

I was struggling with this for a while...how to set the background of a bitmap.  I read a few suggestions, but none worked for me. 

My need was to set the background of the image to white, crop it, with the end result that after cropping, the background be set to white.

To do this, I used the Clear method on the Graphics class...essentially this sets the background.

 

Graphics grPhoto = Graphics.FromImage(bmpImage);

grPhoto.Clear(Color.White);

grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();

Cliff Gray's Info

Cliff Gray
Developer/Founder GrayTechnology.com.

E-mail me Send mail

Authors

Calendar

<<  June 2017  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

Blogroll

Download BlogEngine.NET

Download at CodePlex

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Subscribe