从3个图片框(不包含图像)中生成一个位图(图像)

本文关键字:图像 位图 一个 3个 包含 | 更新日期: 2023-09-27 18:09:33

在我的表单中,我有3个图片框。这3个图片框具有在其表面绘制的油漆事件。现在我想把3个图片框的内容组装到一个位图上,这样我就可以把完整的东西复制到剪贴板上。

然而,我很困惑。我似乎无法将图片框的内容复制到图像中(因为我正在使用paint事件)。

我的代码是这样的:

pbPictureBox1_paint(object sender, PaintEventArgs e)
{
  e.graphics.whatever;  // there's a _lot_ of drawing happening here.
}
pbPictureBox2_paint(object sender, PaintEventArgs e)
{
  e.graphics.whatever;  // there's a _lot_ of drawing happening here.
}
pbPictureBox3_paint(object sender, PaintEventArgs e)
{
  e.graphics.whatever;  // there's a _lot_ of drawing happening here.
}

现在,我想把所有图片框的内容复制到位图上,这样我就可以把它复制到剪贴板上。我可以创建一个正确大小的新位图,并将其复制到剪贴板上。但无论我怎么做,我似乎都无法将pboxes的内容复制到位图中。

Bitmap ClipboardBitmap = new Bitmap(correctwidth, correctheight);
// here, I want to copy the contents of pbox1 to some position 0,y of the bitmap
// here, I want to copy the contents of pbox2 to some position x,0 of the bitmap
// here, I want to copy the contents of pbox3 to some position x,y of the bitmap
Clipboard.SetImage(ClipboardBitmap);

前一段时间,我在我的图片盒上画画,首先创建一个图像,在图像上画画,然后把它分配给图片盒。然而,这太慢了(有很多正在进行的绘画),所以我不得不求助于_paint事件。但是现在我再也不能把我的图形从图片框中取出来了。怎么做呢?

从3个图片框(不包含图像)中生成一个位图(图像)

在折腾了3个小时之后,没有让它工作,当然我在最终决定发布问题后的10分钟内就弄清楚了。

方法如下;

            // make new bitmap
            Bitmap ClipboardBitmap = new Bitmap(width, height);
            // copy content of pb1 to position 0, y of bitmap
            Rectangle pb1Area = new Rectangle(0, y, labelwidth, labelheigth);
            pb1.DrawToBitmap(ClipboardBitmap, pb1Area);
            // same for pb2 and pb3...
            // copy bitmap to clipboard
            Clipboard.SetImage(ClipboardBitmap);