private static Bitmap GetScreenShot(Control ctrl)
    {
      Bitmap result = new Bitmap(ctrl.Width, ctrl.Height, PixelFormat.Format32bppArgb);
      using (Graphics gfx = Graphics.FromImage(result))
      {
        Point pt = ctrl.PointToScreen(new Point(0, 0));
        gfx.CopyFromScreen(pt.X, pt.Y, 0, 0, result.Size, CopyPixelOperation.SourceCopy);
      }
      return result;
    }
    private void button7_Click(object sender, EventArgs e)
    {
      Bitmap panelScreenshot = GetScreenShot(panel1);
      pictureBox1.Image = panelScreenshot;
      panelScreenshot.Save(@"C:\picture.bmp", ImageFormat.Bmp);
    }

Edited 6 Years Ago by sknake: n/a

Comments
nice...never knew you could do that :D

but if the panel has a scrollbar the saved picture will be about the shown site I want to save the all the panel

Try explaining what you're doing in more detail. Why does this need to be a picture? Are you trying to print the contents of the panel? What kind of controls are in the panel?

If a Panel as you call it, is in fact a PictureBox, just use the Image property to get the whole picture.

yes I am trying to print the contents of the panel? What kind of controls are in the panel?

This sounds like a very bad idea. Are you sure this is the approach you want to take? With printing you need to take a lot of things in to account and if you print controls like that it will basically be a screenshot of the form. You can't cleanly break the image if it spans multiple pages, change report format for paper orientation, etc. It is probably a better idea to extract the values from the controls in the panel and feed it to a report writer.

If you still want to print your panel I think you're going to have to work some kind of magic to build the image. I don't know how to print a form or control that has scrollable regions not showing from a screen capture. You could programatically set the vertical scroll position to 0, screenshot the panel, scroll the panel down, take another screen shot, etc and combine the image to get one long image. You can freeze screen updates during this process so the user shouldn't even see what is being done under the hood.

This question has already been answered. Start a new discussion instead.