-2

How can i create screenshot in c# Windows Forms App?

I tried:

ScreenCapture sc = new ScreenCapture();       
Image img = sc.CaptureScreen();
this.imageDisplay.Image = img;
sc.CaptureWindowToFile(this.Handle, "C:\\temp2.png", ImageFormat.png);`

Second Attempt

using (var bitmap = new Bitmap(1680, 1050))
{
   DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
   bitmap.Save(@"c:\TEST.bmp");
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Szabolcs
  • 11
  • 3
  • Have you done any of the `c#` code so far? What have you attempted? – gravity Jul 22 '20 at 13:57
  • I tryed this: `ScreenCapture sc = new ScreenCapture(); // capture entire screen, and save it to a file Image img = sc.CaptureScreen(); // display image in a Picture control named imageDisplay this.imageDisplay.Image = img; // capture this window, and save it sc.CaptureWindowToFile(this.Handle, "C:\\temp2.gif", ImageFormat.Gif);` just this doesent work :D – Szabolcs Jul 22 '20 at 13:58
  • 2
    Please [edit] your question to include what you've tried, and what research you've done. For instance, I put the title of your question into a search engine and one of the first results was [Capture Screenshot Windows Forms by c#](https://stackoverflow.com/q/23356911/215552) – Heretic Monkey Jul 22 '20 at 14:00
  • the code is doesn't work – Szabolcs Jul 22 '20 at 14:04
  • ScreenCapture doesn't seem to be in the standard .net framework? – Carra Jul 22 '20 at 14:06
  • You tried. But what happened (or didn't)? It's hard to troubleshoot a problem that is not explained. – Flater Jul 22 '20 at 14:09
  • 1
    Also, why are you taking what appears to be a full screenshot, showing it in your application, and then taking a screenshot of your application and storing that to disk? I'm not saying that you shouldn't do so, but that is either irrelevant to the question, or it at the very least warrants explanation as to _which of the two captures isn't working_. – Flater Jul 22 '20 at 14:11
  • If "doesn't work" means that it throws an exception, then details are very welcome – Hans Kesting Jul 22 '20 at 14:18
  • https://stackoverflow.com/q/1493591/1070452 many thousands available – Ňɏssa Pøngjǣrdenlarp Jul 22 '20 at 14:22
  • 2
    Does this answer your question? [Capture screenshot of active window?](https://stackoverflow.com/questions/1163761/capture-screenshot-of-active-window) – Heretic Monkey Jul 22 '20 at 14:37

1 Answers1

0

I would have refered you to this post on the same topic Capture screenshot of active window? but it looks like the accepted answer is where you copied your original code from. That code relies on an 3rd party library from 2004 (https://www.developerfusion.com/code/4630/capture-a-screen-shot/) so you need to add that to your project in order to use your code.

But the same question has 10 other answers which does not rely on 3rd party libraries and most of them should work just fine. Like this one for example:

Rectangle bounds = Screen.GetBounds(Point.Empty); 
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) {
    using(Graphics g = Graphics.FromImage(bitmap))
    {
         g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
    }
    bitmap.Save("test.jpg", ImageFormat.Jpeg); 
}

https://stackoverflow.com/a/1163770/612620

This question should probably be marked as a duplicate of Capture screenshot of active window?

JonC
  • 978
  • 2
  • 7
  • 28