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

提出这个问题令人尴尬,却无法找到答案。

我徒劳地试了这个。

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

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

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

所以只需删除该行

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

你几乎拥有它:

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

另一种方法,

创建单位位图

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

并扩大规模

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