C# 如何用纯色填充位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1720160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fill a bitmap with a solid color?
提问by Salvador
I need to create a 24-bit bitmap (resolution 100x100 pixels) using a unique RGB color and save the generated image to the disk. I currently use the SetPixelfunction, but it is extremely slow.
我需要使用唯一的 RGB 颜色创建 24 位位图(分辨率 100x100 像素)并将生成的图像保存到磁盘。我目前正在使用该SetPixel功能,但速度非常慢。
Bitmap Bmp = new Bitmap(width, height);
//...
//...
Bmp.SetPixel(x,y,Color.FromARGB(redvalue, greenvalue, bluevalue));
Is there a faster method than SetPixel? Thanks in advance.
有比 更快的方法SetPixel吗?提前致谢。
采纳答案by Jeromy Irvine
This should do what you need it to. It will fill the entire bitmap with the specified color.
这应该做你需要的。它将用指定的颜色填充整个位图。
Bitmap Bmp = new Bitmap(width, height);
using (Graphics gfx = Graphics.FromImage(Bmp))
using (SolidBrush brush = new SolidBrush(Color.FromArgb(redvalue, greenvalue, bluevalue)))
{
gfx.FillRectangle(brush, 0, 0, width, height);
}
回答by Ryan Cook
It depends on what you are trying to accomplish, but usually you would use GDI+ by getting a graphics object and then drawing to it:
这取决于您要完成的任务,但通常您会通过获取图形对象然后绘制到它来使用 GDI+:
Graphics g = Graphics.FromImage(bitmap);
Its actually a big subject, here are some beginner tutorials: GDI+ Tutorials
它实际上是一个很大的主题,这里有一些初学者教程:GDI+教程
Here is a snippet from the tutorial on drawing a rectangle with a gradient fill.
这是绘制带有渐变填充的矩形的教程中的一个片段。
Rectangle rect = new Rectangle(50, 30, 100, 100);
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lBrush, rect);
回答by Gonzalo
回答by David
I suggest checking out the GD Library.
我建议查看 GD 库。
I'm rather certain there is a c# library. http://www.boutell.com/gd/
我很确定有 ac# 库。 http://www.boutell.com/gd/
回答by Andrew Shepherd
You're spoilt for choice here :-)
你在这里被宠坏了:-)
An alternative to using GDI+ is to use WPF (see RenderTargetBitmap.Render.)
使用 GDI+ 的替代方法是使用 WPF(请参阅RenderTargetBitmap.Render。)
Also see this question.
另请参阅此问题。
回答by Mohamed Abedallah
always Working with regions( rectangle) is much faster Than using individual pixels.
始终使用区域(矩形)比使用单个像素快得多。
回答by Sam Saarian
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Green);
回答by Srivishnu
Creating bitmap object bmp of Size s (height , width) and Color c.
创建 Size s (height , width) 和 Color c 的位图对象 bmp。
bmp = CreateBmp(c, s);
Now CreateBmp method which returns bitmap:
现在返回位图的 CreateBmp 方法:
Bitmap CreateBmp(Color c, Size s)
{
Bitmap temp =new Bitmap(1, 1);
temp.SetPixel(0, 0, c);
return new Bitmap(temp, s);
}

