Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...