Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

c# - How do I save several pictureboxes inside a panel?

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);                                                  

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...