[英]How do I fill a bitmap with a solid color?
I need to create a 24-bit bitmap (resolution 100x100 pixels) using a unique RGB color and save the generated image to the disk.我需要使用唯一的 RGB 颜色创建 24 位位图(分辨率 100x100 像素)并将生成的图像保存到磁盘。 I currently use the
SetPixel
function, but it is extremely slow.我目前使用
SetPixel
函数,但它非常慢。
Bitmap Bmp = new Bitmap(width, height);
//...
//...
Bmp.SetPixel(x,y,Color.FromARGB(redvalue, greenvalue, bluevalue));
Is there a faster method than SetPixel
?有没有比
SetPixel
更快的方法? Thanks in advance.提前致谢。
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);
}
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Green);
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);
您可以使用LockBits来加速写入像素(指针访问而不是每个像素的方法调用)。
始终使用区域(矩形)比使用单个像素快得多。
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 .另请参阅此问题。
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);
}
I suggest checking out the GD Library.我建议查看 GD 库。
I'm rather certain there is ac# library.我很确定有 ac# 库。 http://www.boutell.com/gd/
http://www.boutell.com/gd/
是或不是:透明是一种颜色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.