There are (at least) two ways to do it:
- The one your in your code will work fine if the
PictureBoxes
can actually show the images without cutting them off. Note: The PictureBoxes
must really sit inside the Panel
(i.e. it must be their Parent
) or else it will not draw them!
Here is a working example:
private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);
using (Graphics G = Graphics.FromImage(bmp))
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
// now we can save it..
bmp.Save("d:\foursome.jpg", ImageFormat.Jpeg);
// and let it go:
bmp.Dispose();
}
- The other way uses
DrawImage
to draw the Images
in code.
It is more complicated but gives you more control:
private void button2_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);
int x1 = 0;
int x2 = Math.Max(pictureBox1.Image.Width, pictureBox3.Image.Width);
int y1 = 0;
int y2 = Math.Max(pictureBox1.Image.Height, pictureBox2.Image.Height);
Rectangle rect1 = new Rectangle(new Point(x1, y1), pictureBox1.Image.Size);
Rectangle rect2 = new Rectangle(new Point(x2, y1), pictureBox2.Image.Size);
Rectangle rect3 = new Rectangle(new Point(x1, y2), pictureBox3.Image.Size);
Rectangle rect4 = new Rectangle(new Point(x2, y2), pictureBox4.Image.Size);
using (Graphics G = Graphics.FromImage(bmp))
{
G.DrawImage(pictureBox1.Image, rect1);
G.DrawImage(pictureBox2.Image, rect2);
G.DrawImage(pictureBox3.Image, rect3);
G.DrawImage(pictureBox4.Image, rect4);
}
bmp.Save("d:\foursome2jpg", ImageFormat.Jpeg);
// and clean up:
bmp.Dispose();
}
This will not only let you add or remove spacing between the Images but also lets you resize them by using this DrawImage format. And of course you could add whatever you want to, eg.g fancy frames or text..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…