如何在面板中保存几个图片框? [英] How do I save several pictureboxes inside a panel?
本文介绍了如何在面板中保存几个图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确实有一个带有多个控件和4张图像的面板.这4张图像在框架内.现在,我只想将4个图片框(在框架中)保存为jpg文件,但是这些图片框都是白色的,在保存的图像中我只能看到面板.
I do have a panel with several controls and 4 images. The 4 images are inside a frame. Now I would like to save ONLY the 4 pictureboxes (in the frame) into jpg file but the pictureboxes are all white and I see only the panel in the saved image.
Bitmap bmp = new Bitmap(frame.Width, frame.Height);
Rectangle rect = new Rectangle(0, 0, frame.Width, frame.Height);
this.panel1.DrawToBitmap(bmp, rect);
bmp.Save("C:\\Temp\\zo.jpg", ImageFormat.Jpeg);
我该怎么办?
推荐答案
(至少)有两种方法可以做到:
There are (at least) two ways to do it:
- 如果
PictureBoxes
可以实际显示图像而不将其剪切掉,则您的代码中的代码就可以正常工作.注意:PictureBoxes
必须确实位于内侧Panel
(即必须是其Parent
)内,否则它不会画出它们!
- The one your in your code will work fine if the
PictureBoxes
can actually show the images without cutting them off. Note: ThePictureBoxes
must really sit inside thePanel
(i.e. it must be theirParent
) or else it will not draw them!
这是一个有效的示例:
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();
}
- 另一种方法是使用
DrawImage
在代码中绘制Images
. - The other way uses
DrawImage
to draw theImages
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..
这篇关于如何在面板中保存几个图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文