简体   繁体   中英

How can I change the background color of an image using GDI+?

我想知道在动态生成图像时如何更改背景颜色。

Just use the Graphics object .Clear() method, passing the color you wish to use for the background.

For example:

g.Clear(Color.Blue);

It's simple:

Graphics graph = Graphics.FromImage(bitmap);
graph.Clear(Color.Yellow); // set color for background

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM