开发者

C# Forms Picturebox to show a solid color instead of an image

Im making a little app to display the pictures of guests as they scan their cards. But i want to to display blank green or red (green if the guest exists without a photo and red if they dont exist)

But i cant figure out how to create a blank colour image.

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

Thats the code i have at the moment and it just makes the box black. imgbox开发者_如何学Go is a PictureBox


Don't use an image - set the BackColor property of the PictureBox:

imgBox.BackColor = Color.Red;


To prevent null pointer exception, create a blank 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));


How about setting the background color directly?

imgbox.BackColor = Color.Red;


create a graphics context and draw using it.

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


Single statement:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜