4

i have one picture showing of the human body and i want to use shapes to locate the injuries of the patient. all the shapes will shows off when the user click button. right now im testing with only one shape.

here is my code.

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();
    }

right now, the shape appear at the back of the image. so how i want to make the shape appear in front of the picture?

enter image description here

4

2 回答 2

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));
  }
}
于 2012-09-26T02:05:12.267 回答
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 
于 2012-09-26T04:53:40.810 回答