snapshot on C# (without monitor or even graphic card installed)
I'm trying to record all what happens in my little program(video and audio). I've found on forums people mentioning that there can be a big problem if screenshot is made on the machine without monitor - no monitor physically connected or when graphics card places monitor in sleep mode or even without graphics card installed (servers). is there a way to solve this is开发者_高级运维sue? currently i haven't access to server and even don't know it's configuration(((
currently i'm using this code to make snapshots every 50 milliseconds and it's works great on desktop pc(can't check this code without monitor... i'm using HP touchsmart monoblock desktop)
IntPtr myIntptr = FormElement.Handle;
int hwndInt = myIntptr.ToInt32();
IntPtr hwnd = myIntptr;
Bitmap bm = new Bitmap(FormElement.Width, FormElement.Height);
Graphics g = Graphics.FromImage(bm);
IntPtr hdc = g.GetHdc();
bool result = PrintWindow(hwnd, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();
if (result == true)
{
bm.Save(aFileName, ImageFormat.Jpeg);
}
this doent work if i minimize my window and this is not good too.
sorry for my english and thanx for any advice.
Try like this:
public static void ScreenCapture(string filename, int width, int height)
{
var bounds = new Rectangle(0, 0, width, height);
using (var bitmap = new Bitmap(bounds.Width, bounds.Height))
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(
new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size
);
bitmap.Save(filename, ImageFormat.Jpeg);
}
}
精彩评论