代码之家  ›  专栏  ›  技术社区  ›  dkarzon Sven Grosen

C窗体PictureBox以显示纯色而不是图像

  •  6
  • dkarzon Sven Grosen  · 技术社区  · 15 年前

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

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

    Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb);
    imgbox.Image = bmRed;
    

    这就是我现在拥有的代码,它只是让这个盒子变黑。 imgBox是一个图片框

    5 回复  |  直到 7 年前
        1
  •  12
  •   Jay Riggs    15 年前

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

    imgBox.BackColor = Color.Red;
    
        2
  •  4
  •   Michael_S_    13 年前

    要防止空指针异常,请创建一个空白的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));
    
        3
  •  2
  •   micahtan    15 年前

    直接设置背景色怎么样?

    imgbox.BackColor = Color.Red;
    
        4
  •  1
  •   Andrew Keith    15 年前

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

    using(Graphics g = Graphics.FromImage(bmRed))
    {
      g.FillRectangle(new SolidBrush(Color.Red),0,0,imgbox.Width,imgbox.Height);
    }
    
        5
  •  -2
  •   Bugs Manjeet    7 年前

    单个语句:

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