C#Forms Picturebox显示纯色而不是图像

dka*_*zon 7 c# windows image

我正在制作一个小应用程序来显示客人扫描卡片时的照片.但我想显示空白的绿色或红色(如果客人没有照片则为绿色,如果不存在则为红色)

但我无法弄清楚如何创建一个空白的彩色图像.

Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb);
imgbox.Image = bmRed;
Run Code Online (Sandbox Code Playgroud)

这就是我现在的代码,它只是让盒子变黑.imgbox是一个PictureBox

Jay*_*ggs 13

不要使用图像 - 设置PictureBox的BackColor属性:

imgBox.BackColor = Color.Red;
Run Code Online (Sandbox Code Playgroud)


Mic*_*_S_ 6

为了防止空指针异常,创建一个空白bmp

myPicBox.Image = new Bitmap(myPicBox.Width, myPicBox.Height);
Graphics graphics = Graphics.FromImage(myPicBox.Image);

Brush brush = new SolidBrush(Color.Gray);

graphics.FillRectangle(brush, new System.Drawing.Rectangle(0, 0, myPicBox.Width, myPicBox.Height));
Run Code Online (Sandbox Code Playgroud)