c# - 并行更新 2 个图片框

标签 c# multithreading picturebox

我有 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/

相关文章:

c# - 将 Drawline 图像绘制到 Picturebox 图像中

printing - 写入图片盒和打印机时避免重复

c# - 将 Generic<Derived> 转换为 Generic<Base>

c# - 'out' 参数与通用/模板参数 : best practice

java - 如何从字符串变量中的转义序列中转义

c++ - 递归 pthread 生成 - 堆栈位置

c# - 显示/隐藏 PictureBox 内的按钮

c# - DataContractSerializer 未正确反序列化,缺少对象中方法的值

.net - Thread.Yield() 会导致 CPU 峰值吗?

linux - 如何使用 strace 跟踪子进程?