并行更新2个图片框

Mat*_*ont 5 c# multithreading picturebox

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

现在我有这个:

picturebox_1.Refresh();
picturebox_2.Refresh();
Run Code Online (Sandbox Code Playgroud)

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

Picturebox 1:

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

Picturebox 2:

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

是否有捷径可寻?我是线程等新手.

谢谢!

War*_*enG 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));
}
Run Code Online (Sandbox Code Playgroud)

编辑:额外的例子

我将每个位图存储在字典中,以图片框作为键,如下所示:

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.
    }
}
Run Code Online (Sandbox Code Playgroud)