|
-
Nov 4th, 2009, 03:04 PM
#1
Thread Starter
Addicted Member
[RESOLVED]Screenshot, without screen?
Hello,
I have a pc, without a monitor attached to it. I want to make a screenshot once in x minutes, and then upload the image. The problem is, I cannot make a screenshot, because there is no screen?
I tried this code:
Code:
public Image CaptureScreen()
{
System.Windows.Forms.SendKeys.SendWait("{PRTSC}");
return System.Windows.Forms.Clipboard.GetImage();
}
I don't get anything this way.
I also tried this (Also with hardcoded values, 800*600), and don't get anything.
Code:
public Image CaptureScreen()
{
Bitmap bmpScreenshot;
Graphics gfxScreenshot;
// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
return bmpScreenshot;
}
And this (Which gives me a black 800*600 file)
Code:
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
public Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
}
Is there any other way to make a screenshot? Is there even a way to do it without having a screen?
With kind regards,
Thomas
Last edited by Mindstorms; Nov 15th, 2009 at 12:28 PM.
Reason: Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|