2013-01-23 113 views
1

我應該從一個文件加載一個圖像,這個圖像應該覆蓋80%的圖片框,然後在它上面畫一些東西......加載沒有問題,但試圖繪製任何東西滴具有不合適參數的錯誤(g.FillRectangle ...)。加載的圖片編輯

我堆棧建議發現刷新圖片框,但它改變不了什麼...
,我不知道如何解決這個問題?

private void button1_Click_1(object sender, EventArgs e) 
{ 
    pictureBox1.Width = (int)(Width * 0.80); 
    pictureBox1.Height = (int)(Height * 0.80); 

    // open file dialog 
    OpenFileDialog open = new OpenFileDialog(); 
    // image filters 
    open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
    if (open.ShowDialog() == DialogResult.OK) 
    { 
     // display image in picture box 
     pictureBox1.Image = new Bitmap(open.FileName); 
     // image file path 
     // textBox1.Text = open.FileName; 
     g.FillRectangle(Brushes.Red, 0, 0, 20, 50); 
     pictureBox1.Refresh(); 
    } 
} 
+0

爲什麼不其加載到圖片框前的位圖圖像上繪製? – Arjang

+0

變量'g'從哪裏來?我假設它意味着代表一個'Graphics'實例? –

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

使用Graphics.FromImageControl.CreateGraphics方法上畫出你圖像:

var img = new Bitmap(open.FileName); 
using (Graphics g = Graphics.FromImage(img)) 
{ 
    g.FillRectangle(Brushes.Red, 0, 0, 20, 50); 
} 
pictureBox1.Image = img; 

或(通過使用Anonymous Methods例如)直接在PictureBox通過Paint事件繪製:

pictureBox1.Paint += (s, e) => e.Graphics.FillRectangle(Brushes.Red, 0, 0, 20, 50); 
0

下面的代碼爲我工作的罰款....你可以嘗試同樣的

private void button1_Click(object sender, EventArgs e) 
     { 
      pictureBox1.Width = (int)(Width * 0.80); 
      pictureBox1.Height = (int)(Height * 0.80); 


      // open file dialog 
      OpenFileDialog open = new OpenFileDialog(); 

      // image filters 
      open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
      if (open.ShowDialog() == DialogResult.OK) 
      { 
       // display image in picture box 
       pictureBox1.Image = new Bitmap(open.FileName); 
       // image file path 
       // textBox1.Text = open.FileName; 
       Graphics g = Graphics.FromImage(pictureBox1.Image); 
       g.FillRectangle(Brushes.Red, 0, 0, 20, 50); 
       pictureBox1.Refresh(); 
      } 
     } 
+0

大家好! Graphics g = Graphics.FromImage(pictureBox1.Image); 這個工程!我不知道爲什麼我的Graphic g的實現沒有起作用,它直接在整個程序開始時執行.... – argh