如何使用GDI +更改图像的背景颜色?

时间:2022-11-20 21:33:45

I want to know how to change the background color when I generate the image dynamically.

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

3 个解决方案

#1


27  

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

只需使用Graphics对象.Clear()方法,传递您希望用于背景的颜色。

For example:

g.Clear(Color.Blue);

#2


2  

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.

如果你在谈论一个特定的文件格式的“背景颜色”字段,我不确定GDI +是否支持它,但通常设置图像的背景颜色,你用一种颜色填充一个矩形的图像大小。

Example, assuming g is your Graphics object, image is your Image object, and color is your Color object:

例如,假设g是您的Graphics对象,image是您的Image对象,color是您的Color对象:

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

此外,正如FlipScript建议的那样,您可以使用Clear方法。 (我不知道它存在!)

g.Clear(color);

#3


2  

It's simple:

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

#1


27  

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

只需使用Graphics对象.Clear()方法,传递您希望用于背景的颜色。

For example:

g.Clear(Color.Blue);

#2


2  

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.

如果你在谈论一个特定的文件格式的“背景颜色”字段,我不确定GDI +是否支持它,但通常设置图像的背景颜色,你用一种颜色填充一个矩形的图像大小。

Example, assuming g is your Graphics object, image is your Image object, and color is your Color object:

例如,假设g是您的Graphics对象,image是您的Image对象,color是您的Color对象:

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

此外,正如FlipScript建议的那样,您可以使用Clear方法。 (我不知道它存在!)

g.Clear(color);

#3


2  

It's simple:

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