我正在制作一个小应用程序,用于在客人刷卡时显示他们的照片。 但是我想显示空白的绿色或红色(如果客人存在但没有照片则为绿色,如果不存在则为红色)。
但是我不知道如何创建一个空白颜色的图像。
Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb);
imgbox.Image = bmRed;
这是我目前的代码,它只会让盒子变成黑色。 imgbox 是一个 PictureBox。
不要使用图像 - 设置PictureBox的BackColor属性:
imgBox.BackColor = Color.Red;
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));
imgbox.BackColor = Color.Red;
using(Graphics g = Graphics.FromImage(bmRed))
{
g.FillRectangle(new SolidBrush(Color.Red),0,0,imgbox.Width,imgbox.Height);
}
单语句:
Graphics.FromImage(PicBox.Image=new bitmap(PicBox.Size)).FillRectangle(Brushes.Red,new Rectangle (Point.EMPTY,PicBox.Size));