2

I need a solution how could i make a Print Screen of my WinForm on C# and export it as PNG.

Bests

Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89

2 Answers2

3

I think this blog post will help.

using (Bitmap bitmap = new Bitmap(ParentForm.Size.Width, ParentForm.Size.Height))
{
    using (Graphics g = Graphics.FromImage(bitmap))
    {
      g.CopyFromScreen(new Point(ParentForm.DesktopLocation.X, ParentForm.DesktopLocation.Y), new Point(0, 0), ParentForm.Size);
    }

    bitmap.Save(@"C:\test.jpg", ImageFormat.Jpeg);
}
Jake Pearson
  • 27,069
  • 12
  • 75
  • 95
  • 1
    If the window doing this isn't the frontmost, won't this copy whatever might be in front of it? – cHao Jan 05 '11 at 15:32
  • thanks a lot ,that helped and worked after doin a lot of search .But as cHao is saying if Form is not TopMost it will not work ,what means if i minimize the Form that will print only a black square . – Rosmarine Popcorn Jan 05 '11 at 15:39
  • Yep, I just tried it. I think the form needs to be active for this trick to work. So this will work fine if you add a button to your form that copies the screen, but if you want to capture a different form or with a shortcut key, you will probably need to use a different mechanism to capture. – Jake Pearson Jan 05 '11 at 15:43
  • well i need to do that Automatically ,and i`m playing with WindowsFormState when i need to export ,so that's OK For me ,it's working and im enjoining the Quality .Bests – Rosmarine Popcorn Jan 07 '11 at 13:58
1

Never tried it, but i'd think you should be able to call OnPaint(args) with a PaintEventArgs you create, that includes a Graphics for the image you want to draw on, and the ClipRectangle encompassing the whole area of the form.

This would only work if your form properly processes paint messages (ie: if it stores enough info to be able to repaint the window fully at will), and it may only get the client area (ie: it might not get the title bar or menus).

cHao
  • 84,970
  • 20
  • 145
  • 172