C# 如何创建 1024x1024 RGB 白色位图图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12502365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:28:28  来源:igfitidea点击:

How to create 1024x1024 RGB bitmap image of white?

c#

提问by Tae-Sung Shin

It's embarrassing to ask this question but can't find an answer.

问这个问题很尴尬,但找不到答案。

I tried this in vain.

我徒劳地尝试了这个。

Image resultImage = new Bitmap(image1.Width, image1.Height, PixelFormat.Format24bppRgb);

using (Graphics grp = Graphics.FromImage(resultImage)) 
{
    grp.FillRectangle(
        Brushes.White, 0, 0, image1.Width, image1.Height);
    resultImage = new Bitmap(image1.Width, image1.Height, grp);
}

I basically want to fill a 1024x1024 RGB bitmap image with white in C#. How can I do that?

我基本上想在 C# 中用白色填充 1024x1024 RGB 位图图像。我怎样才能做到这一点?

采纳答案by Joey

You are assigning a new image to resultImage, thereby overwriting your previous attempt at creating a white image (which should succeed, by the way).

您正在为 分配一个新图像resultImage,从而覆盖您之前创建白色图像的尝试(顺便说一下,这应该会成功)。

So just remove the line

所以只需删除该行

resultImage = new Bitmap(image1.Width, image1.Height, grp);

回答by Lee Harrison

You almost had it:

你几乎拥有它:

private Bitmap DrawFilledRectangle(int x, int y)
{
    Bitmap bmp = new Bitmap(x, y);
    using (Graphics graph = Graphics.FromImage(bmp))
    {
        Rectangle ImageSize = new Rectangle(0,0,x,y);
        graph.FillRectangle(Brushes.White, ImageSize);
    }
    return bmp;
}

回答by prashanth

Another approach,

另一种做法,

Create a unit bitmap

创建单位位图

var b = new Bitmap(1, 1);
b.SetPixel(0, 0, Color.White);

And scale it

并缩放它

var result = new Bitmap(b, 1024, 1024);

回答by Top Systems

Graphics.Clear(Color)

图形.清除(颜色)

Bitmap bmp = new Bitmap(1024, 1024);
using (Graphics g = Graphics.FromImage(bmp)){g.Clear(Color.White);}