2012-09-26 27 views
3

我有一張圖片顯示人體,我想用形狀來定位病人的傷勢。當用戶點擊按鈕時,所有的形狀都會顯示出來。現在我只用一種形狀進行測試。將形狀放置在c中的圖像前#

這是我的代碼。

private void button7_Click_4(object sender, EventArgs e) 
    { 
     Graphics g = this.CreateGraphics(); 
     g.Clear(this.BackColor); 

     Image img = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png"); 
     g.DrawImage(img, 0, 0, img.Height, img.Width); 
     g.Dispose(); 
    } 

現在,形狀出現在圖像的背面。所以我想如何讓這個形狀出現在圖片前?

enter image description here

+0

我想你可能會首先繪製形狀,然後是圖像。你能告訴我們你用來繪製形狀的代碼嗎? – 3aw5TZetdf

回答

4

幾個問題。

1)繪畫應該發生在繪畫事件中。不要使用CreateGraphics,因爲那隻會是一個臨時圖形。

2)你的DrawImage寬度和高度參數是相反的。

3)它看起來並不像你畫你有窗體上的PictureBox控件:

private Image img; 

public Form1() { 
    InitializeComponent(); 
    button1.Click += button1_Click; 
    pictureBox1.Paint += pictureBox1_Paint; 
} 

void button1_Click(object sender, EventArgs e) { 
    img = = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png"); 
    pictureBox1.Invalidate(); 
} 

void pictureBox1_Paint(object sender, PaintEventArgs e) { 
    e.Graphics.Clear(pictureBox1.BackColor); 

    if (img != null) { 
    e.Graphics.DrawImage(img, 0, 0, img.Width, img.Height); 

    //Draw test shape: 
    e.Graphics.DrawRectangle(Pens.Red, new Rectangle(10, 10, 20, 60)); 
    } 
} 
+0

我複製粘貼你的代碼到我的。它仍然出現在圖片背面的形狀..我犯了錯誤嗎? –

+0

@sarabrown如果它不起作用,那麼你有代碼運行,我們看不到。有一點很突出,你的代碼似乎是在表單本身上繪製的,你也有明顯的按鈕。一般來說,你應該有一個Panel或一個PictureBox控件來顯示這個圖像。 – LarsTech

+0

即時通訊工具箱中使用的組件,我只拖動任何組件到我希望使用的地方。圖片來自圖片框並放置在面板內。我也編輯我的帖子,以獲得更好的觀點。希望能幫助到你。 –

0

我想你應該先把人形象的圖形,然後在其上繪製形狀圖像。有些事情是這樣的:

Image img = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png"); 

Graphics g = Graphics.FromImage (img); 

g.DrawImage(ShapeImage, 0, 0, 30, 30); // you can set your required x,y,width,height 
+0

它不會工作,它出現在圖片的背面。你有沒有其他的解決方案,或者可能是以一種錯誤的方式去做? –