并行更新2个图片框

时间:2014-06-10 02:52:07

标签: c# multithreading picturebox

我有2个图片框我想并行更新。

现在我有了这个:

picturebox_1.Refresh();
picturebox_2.Refresh();

在每个油漆事件中我都有这样的东西:

Picturebox 1:

e.Graphics.Clear(System.Drawing.Color.Black);    
e.Graphics.DrawImage(mybitmap1, X, Y); 
e.Graphics.DrawLine(mypen, verticalstart, verticalend); //Draw Vertical

Picturebox 2:

e.Graphics.Clear(System.Drawing.Color.Black);    
e.Graphics.DrawImage(mybitmap2, X, Y); 
e.Graphics.DrawLine(mypen, verticalstart, verticalend);//Draw Vertical line.

有一种简单的方法吗?我是线程等的新手。

谢谢!

1 个答案:

答案 0 :(得分:0)

你能为两个图片框使用相同的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.
    }
}