如何在面板中保存几个图片框? [英] How do I save several pictureboxes inside a panel?

查看:66
本文介绍了如何在面板中保存几个图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有一个带有多个控件和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: The PictureBoxes must really sit inside the Panel (i.e. it must be their Parent) 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();
}

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆