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

Tae*_*hin 29 c#

提出这个问题但是找不到答案令人尴尬.

我徒劳地试了这个.

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);
}
Run Code Online (Sandbox Code Playgroud)

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

Lee*_*son 35

你几乎拥有它:

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;
}
Run Code Online (Sandbox Code Playgroud)


Joe*_*oey 28

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

所以只需删除该行

resultImage = new Bitmap(image1.Width, image1.Height, grp);
Run Code Online (Sandbox Code Playgroud)


pra*_*nth 21

另一种方法,

创建单位位图

var b = new Bitmap(1, 1);
b.SetPixel(0, 0, Color.White);
Run Code Online (Sandbox Code Playgroud)

并扩大规模

var result = new Bitmap(b, 1024, 1024);
Run Code Online (Sandbox Code Playgroud)

  • 小心!当你使用`Color.FromArgb()`时,你会在缩放位图时得到一个渐变.更好地使用@LeeHarrison的[解决方案](http://stackoverflow.com/a/12502497/489772). (4认同)

Top*_*ems 6

图形.清除(彩色)

Bitmap bmp = new Bitmap(1024, 1024);
using (Graphics g = Graphics.FromImage(bmp)){g.Clear(Color.White);}
Run Code Online (Sandbox Code Playgroud)