2013-01-23 82 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