Reputation: 86185
I want to know how to change the background color when I generate the image dynamically.
Upvotes: 16
Views: 25702
Reputation: 41
It's simple:
Graphics graph = Graphics.FromImage(bitmap);
graph.Clear(Color.Yellow); // set color for background
Upvotes: 4
Reputation: 129139
If you're talking about a specific file format's "background color" field, I'm not sure if GDI+ supports that, but usually to set the background color of an image you'd fill a rectangle the size of the image with one color.
Example, assuming g
is your Graphics
object, image
is your Image
object, and color
is your Color
object:
g.FillRectangle(new SolidBrush(color), new Rectangle(Point.Empty, image.Size));
Also, as FlipScript suggested, you can use the Clear
method. (I had no idea it existed!)
g.Clear(color);
Upvotes: 2
Reputation: 4401
Just use the Graphics object .Clear()
method, passing the color you wish to use for the background.
For example:
g.Clear(Color.Blue);
Upvotes: 34