如何在Picturebox上留下照片?

时间:2011-04-21 17:56:30

标签: c# windows-mobile picturebox graphing

我正在Windows Mobile智能设备应用程序的图片框上绘制图表。该图表效果很好,但之后会立即消失。

我用了这句话:

this.Invoke(new EventHandler(picture2_show));

和我的绘图方法:

private void picture2_show(object sender, EventArgs e)
{
    using (Graphics objGraphics = this.pictureBox2.CreateGraphics())
    {
        Pen redpen = new Pen(Color.Red, 1);
        int picBoxWidth = pictureBox2.Size.Width;
        int picBoxHeight = pictureBox2.Size.Height;
        int halfWidth = pictureBox2.Size.Width / 2;
        int halfHeight = pictureBox2.Size.Height / 2;

        Graphics objGraphic = this.pictureBox2.CreateGraphics();

        objGraphic.DrawLine(redpen, 0, 0, 0, picBoxHeight);
        objGraphic.DrawLine(redpen, 0, picBoxHeight - 1, picBoxWidth, picBoxHeight - 1);
        //Rectangle first = new Rectangle(0, halfHeight, picBoxWidth, halfHeight);

        Pen bpen = new Pen(Color.LawnGreen, 3);
        for (int i = 0; i < array.Length - 1; i++)
        {
            objGraphic.DrawLine(
                bpen, 
                pictureBox2.Size.Width * i / array.Length,
                pictureBox2.Size.Height - array[i],
                pictureBox2.Size.Width * (i + 1) / array.Length,
                pictureBox2.Size.Height - array[i + 1]);
        }
    }
}

图片框保留,但如何让图表不会消失?

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

您需要使用Paint在图片框的e.Graphics事件中绘制图表。
要强制重绘,请致电pictureBox2.Invalidate()

请勿在{{1​​}}上绘图,因为下次绘制控件时它将被删除。