2009-11-27 61 views
4

我做了一個小應用程序來顯示客人的圖片,因爲他們掃描他們的卡片。 但我想要顯示空白綠色或紅色(綠色,如果客人存在沒有照片和紅色,如果他們不存在)C#形式圖片框顯示一個純色而不是圖像

但我無法弄清楚如何創建一個空白的彩色圖像。

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

這就是我現在的代碼,它只是使箱子變黑。 imgbox是一個圖片框

回答

10

不要使用圖像 - 設置圖片框的背景色屬性:

imgBox.BackColor = Color.Red; 
1

創建一個圖形上下文並使用它繪製。

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

如何直接設置背景顏色?

imgbox.BackColor = Color.Red; 
4

爲了避免空指針異常,創建一個空白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)); 
-2

單語句:

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

對不起,我最初投入。 PicBox.img而不是PicBox.Image – Srivishnu 2017-06-28 16:10:23

+0

圖形類沒有構造函數,如果它有,你仍然必須處理它。 Point.NULL也是錯誤的。自問題被要求測試您的代碼以來,您已經有8年時間了。 – LarsTech 2017-06-28 16:17:01

+0

對不起,我將其更正爲圖形。從圖像 – Srivishnu 2017-06-29 05:32:03

相關問題