如何让图片保留在 Picturebox 上?

发布于 2024-11-02 18:17:52 字数 1333 浏览 4 评论 0原文

我正在 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]);
        }
    }
}

图片框保留,但如何使图形不消失?

非常感谢帮助!

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,
                pictureBox2.Size.Height - array[i + 1]);
        }
    }
}

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

Help is very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-11-09 18:18:01

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

不要在 CreateGraphics() 上绘图,因为下次绘制控件时它将被擦除。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文