Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#形成Picturebox以显示纯色而不是图像_C#_Windows_Image - Fatal编程技术网

C#形成Picturebox以显示纯色而不是图像

C#形成Picturebox以显示纯色而不是图像,c#,windows,image,C#,Windows,Image,我正在制作一个小应用程序来显示客人扫描卡片时的照片。 但是我想显示空白的绿色或红色(如果客人没有照片,则显示绿色,如果他们不存在,则显示红色) 但我不知道如何创建一个空白的彩色图像 Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb); imgbox.Image = bmRed; 这就是我目前掌握的代码,它只会使方框变黑。 imgbox是一个PictureBox创建图形上下文并使用它

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

但我不知道如何创建一个空白的彩色图像

Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb);
imgbox.Image = bmRed;
这就是我目前掌握的代码,它只会使方框变黑。
imgbox是一个PictureBox

创建图形上下文并使用它进行绘制

using(Graphics g = Graphics.FromImage(bmRed))
{
  g.FillRectangle(new SolidBrush(Color.Red),0,0,imgbox.Width,imgbox.Height);
}

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

imgBox.BackColor = Color.Red;

直接设置背景色怎么样

imgbox.BackColor = Color.Red;

要防止空指针异常,请创建一个空白的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));
单一声明:

Graphics.FromImage(PicBox.Image=new bitmap(PicBox.Size)).FillRectangle(Brushes.Red,new Rectangle (Point.EMPTY,PicBox.Size));

对不起,我一开始被放了。PicBox.img而不是PicBox.ImageGraphics类没有构造函数,如果它有构造函数,您仍然必须处理它。Point.NULL也是错误的。自从有人问你测试代码的问题以来,你已经8年了。