如何让图片保留在 Picturebox 上?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
e.Graphics
在图片框的Paint
事件中绘制图形。要强制其重绘,请调用
pictureBox2.Invalidate()
。不要在
CreateGraphics()
上绘图,因为下次绘制控件时它将被擦除。You need to draw your graph in the picturebox's
Paint
event, usinge.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.