如何用纯色填充位图?

发布于 2024-08-11 03:11:34 字数 368 浏览 1 评论 0原文

我需要使用唯一的 RGB 颜色创建 24 位位图(分辨率 100x100 像素)并将生成的图像保存到磁盘。我目前使用 SetPixel 函数,但是它非常慢。

Bitmap Bmp = new Bitmap(width, height);
//...
//...
Bmp.SetPixel(x,y,Color.FromARGB(redvalue, greenvalue, bluevalue));

有没有比 SetPixel 更快的方法?

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 SetPixel function, but it is extremely slow.

Bitmap Bmp = new Bitmap(width, height);
//...
//...
Bmp.SetPixel(x,y,Color.FromARGB(redvalue, greenvalue, bluevalue));

Is there a faster method than SetPixel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流星番茄 2024-08-18 03:11:34

这应该可以满足您的需要。它将用指定的颜色填充整个位图。

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

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);
}
感悟人生的甜 2024-08-18 03:11:34
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Green);
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Green);
不喜欢何必死缠烂打 2024-08-18 03:11:34

这取决于您想要完成的任务,但通常您会通过获取图形对象然后在其上绘图来使用 GDI+:

Graphics g = Graphics.FromImage(bitmap); 

它实际上是一个很大的主题,这里有一些初学者教程: c-sharpcorner.com/UploadFile/mahesh/gdi_plus12092005070041AM/gdi_plus.aspx" rel="noreferrer">GDI+ 教程

以下是绘制渐变填充矩形的教程片段。

Rectangle rect = new Rectangle(50, 30, 100, 100); 
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); 
g.FillRectangle(lBrush, rect); 

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:

Graphics g = Graphics.FromImage(bitmap); 

Its actually a big subject, here are some beginner tutorials: GDI+ Tutorials

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); 
不如归去 2024-08-18 03:11:34

您可以使用 LockBits 来加速写入像素(指针访问而不是每个像素的方法调用)。

You could use LockBits to speedup writing the pixels (pointer access instead of method call per pixel).

天暗了我发光 2024-08-18 03:11:34

这里的选择太多了:-)

使用 GDI+ 的替代方法是使用 WPF(请参阅 RenderTargetBitmap.Render。)

另请参阅 这个问题

You're spoilt for choice here :-)

An alternative to using GDI+ is to use WPF (see RenderTargetBitmap.Render.)

Also see this question.

极度宠爱 2024-08-18 03:11:34

始终使用区域矩形)比使用单个像素快得多。

always Working with regions ( rectangle ) is much faster Than using individual pixels.

小梨窩很甜 2024-08-18 03:11:34

创建尺寸 s(高度、宽度)和颜色 c 的位图对象 bmp。

bmp = CreateBmp(c, s);

现在 CreateBmp 方法返回位图:

Bitmap CreateBmp(Color c, Size s)
{
    Bitmap temp =new Bitmap(1, 1);
    temp.SetPixel(0, 0, c);
    return new Bitmap(temp, s);
}

Creating bitmap object bmp of Size s (height , width) and Color c.

bmp = CreateBmp(c, s);

Now CreateBmp method which returns bitmap:

Bitmap CreateBmp(Color c, Size s)
{
    Bitmap temp =new Bitmap(1, 1);
    temp.SetPixel(0, 0, c);
    return new Bitmap(temp, s);
}
孤星 2024-08-18 03:11:34

我建议查看 GD 图书馆。

我相当确定有 ac# 库。
http://www.boutell.com/gd/

I suggest checking out the GD Library.

I'm rather certain there is a c# library.
http://www.boutell.com/gd/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文