Screen Capture
REF:http://0rz.net/3a0Gm
新聞群組: microsoft.public.dotnet.csharp.general
寄件人: "Per"
日期: Wed, 5 Jan 2005 08:35:38 +0100
當地時間: 2005年1月5日(星期三) 下午3時35分
主旨: Screen capture
回覆作者 | 轉寄 | 列印 | 顯示個別留言 | 顯示原始檔 | 舉報濫用行為
Hi all,
This may be a newbie question, but I can't find the answer anywhere.
I want to make a capture of the screen, or a part of the screen, that
contains the main portion of the form.
Detecting the screen that contain the main portion of the form is pretty
straight forward, but how do I capture what I need in an Image object?
Another thing, how do you make the program move the mouse pointer and
simulate clicks?
Best regards
Andrew Arace 1月17日 下午10時21分 顯示選項
新聞群組: microsoft.public.dotnet.csharp.general
寄件人: "Andrew Arace"
日期: 17 Jan 2005 06:21:20 -0800
當地時間: 2005年1月17日(星期一) 下午10時21分
主旨: Re: Screen capture
回覆作者 | 轉寄 | 列印 | 顯示個別留言 | 顯示原始檔 | 舉報濫用行為
I can't help you on the mouse pointer click simulation,
but i can provide you with some screen capture code:
public class ImagingUtils {
//imports the GDI BitBlt function that enables the background
of the window
//to be captured
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static int ReleaseDC(System.IntPtr hWnd,
System.IntPtr hDC); //modified to include hWnd
///
/// Returns a full desktop screencapture
///
///
desktop.
public static Image GetScreenCapture() {
System.IntPtr desktopDC = GetDC(System.IntPtr.Zero);
Bitmap bm = new
Bitmap(System.Windows.Forms.SystemInformation.VirtualScreen.Width,
System.Windows.Forms.SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(bm);
System.IntPtr bmDC = g.GetHdc();
BitBlt(bmDC, 0, 0, bm.Width,bm.Height, desktopDC, 0, 0,
0x00CC0020 /*SRCCOPY*/);
ReleaseDC(System.IntPtr.Zero, desktopDC);
g.ReleaseHdc(bmDC);
g.Dispose();
return bm;
}
///
/// Returns a capture of the control's current graphics.
///
/// The winforms control to capture
///
public static Image
GetControlImageCapture(System.Windows.Forms.Control ctrl) {
System.IntPtr ctrlCapture = GetDC(ctrl.Handle);
Bitmap bm = new Bitmap(ctrl.Width,
ctrl.Height);
Graphics g = Graphics.FromImage(bm);
System.IntPtr bmDC = g.GetHdc();
BitBlt(bmDC, 0, 0, bm.Width, bm.Height, ctrlCapture, 0, 0,
0x00CC0020 /*SRCCOPY*/);
ReleaseDC(ctrl.Handle, ctrlCapture);
g.ReleaseHdc(bmDC);
g.Dispose();
return bm;
}
}