开发者

How do I make picture stay on Picturebox?

开发者 https://www.devze.com 2023-02-27 12:08 出处:网络
I am drawing a graph on a picturebox on Windows Mobile Smart Device Application. The graph works well, but it disappears immediately afterwards.

I am drawing a graph on a picturebox on Windows Mobile Smart Device Application. The graph works well, but it disappears immediately afterwards.

I used the line:

this.Invoke(new EventHandler(picture2_show));

and my drawing method:

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,
    开发者_Go百科            pictureBox2.Size.Height - array[i + 1]);
        }
    }
}

The picturebox stays, but how do I make the graph not disappear??

Help is very much appreciated!


You need to draw your graph in the picturebox's Paint event, using e.Graphics.
To force it to redraw, call pictureBox2.Invalidate().

Do not draw on CreateGraphics(), since it will be erased when the control is next painted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号