我有 2 个图片框,我想并行更新。
现在我有这个:
picturebox_1.Refresh();
picturebox_2.Refresh();
在每个绘画事件中,我都有这样的东西:
图片框 1:
e.Graphics.Clear(System.Drawing.Color.Black);
e.Graphics.DrawImage(mybitmap1, X, Y);
e.Graphics.DrawLine(mypen, verticalstart, verticalend); //Draw Vertical
图片框 2:
e.Graphics.Clear(System.Drawing.Color.Black);
e.Graphics.DrawImage(mybitmap2, X, Y);
e.Graphics.DrawLine(mypen, verticalstart, verticalend);//Draw Vertical line.
有没有简单的方法来做到这一点?我是线程等方面的新手。
谢谢!
最佳答案
您能否对两个图片框使用相同的 Paint 事件处理程序?如果我将两个图片框放在一个窗体上并将它们都设置为使用如下所示的处理程序,它们都会显示一条垂直的红线将框一分为二。
private void pictureBox_Paint(object sender, PaintEventArgs e) {
PictureBox pb = sender as PictureBox;
if (pb == null) {
return;
}
Pen p = new Pen(Brushes.Red);
e.Graphics.DrawLine(p, new Point(pb.Width / 2, 0), new Point(pb.Width / 2, pb.Height));
}
编辑:额外的例子
我会将每个位图存储在以图片框为键的字典中,如下所示:
public partial class Form1 : Form {
private Dictionary<PictureBox, Bitmap> bitmaps = new Dictionary<PictureBox,Bitmap>();
public Form1() {
InitializeComponent();
bitmaps.Add(pictureBox1, mybitmap1);
bitmaps.Add(pictureBox2, mybitmap2);
}
private void pictureBox_Paint(object sender, PaintEventArgs e) {
PictureBox pb = sender as PictureBox;
if (pb == null) {
return;
}
e.Graphics.Clear(System.Drawing.Color.Black);
e.Graphics.DrawImage(bitmaps[pb], X, Y);
e.Graphics.DrawLine(mypen, verticalstart, verticalend);//Draw Vertical line.
}
}
关于c# - 并行更新 2 个图片框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132102/