c#使用API截屏类
来源:互联网 发布:算法经典书籍推荐 编辑:程序博客网 时间:2024/05/21 15:04
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace Capture
...{
/**////
/// This class shall keep the GDI32 APIs used in our program.
///
public sealed class GDI32
...{
public const int SRCCOPY = 13369376;
Class Functions#region Class Functions
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern bool BitBlt(
IntPtr hdcDest, //目的DC的句柄
int nXDest, //目的图形的左上角的x坐标
int nYDest, //目的图形的左上角的y坐标
int nWidth, //目的图形的矩形宽度
int nHeight, //目的图形的矩形高度
IntPtr hdcSrc, //源DC的句柄
int nXSrc, //源图形的左上角的x坐标
int nYSrc, //源图形的左上角的x坐标
System.Int32 dwRop //光栅操作代码
);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap
(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
#endregion
}
/**////
/// This class shall keep the User32 APIs used in our program.
///
public sealed class User32
...{
Class Variables#region Class Variables
public const int SM_CXSCREEN = 0;
public const int SM_CYSCREEN = 1;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
...{
public int left;
public int top;
public int right;
public int bottom;
}
#endregion
Class Functions#region Class Functions
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("user32.dll", EntryPoint = "GetWindowDC")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
#endregion
}
/**////
/// This class shall keep all the functionality for capturing
/// the desktop.
///
public class CaptureScreen
...{
Class Variable Declaration#region Class Variable Declaration
protected static IntPtr m_HBitmap;
#endregion
/**//// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public static Image CaptureDesktop()
...{
return CaptureWindow(User32.GetDesktopWindow());
}
/**//// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">
/// The handle to the window. (In windows forms, this is obtained by the Handle property)
/// </param>
/// <returns></returns>
public static 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;
}
/**//// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public static void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
...{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
/**//// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public static void CaptureScreenToFile(string filename, ImageFormat format)
...{
Image img = CaptureDesktop();
img.Save(filename, format);
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace Capture
...{
/**////
/// This class shall keep the GDI32 APIs used in our program.
///
public sealed class GDI32
...{
public const int SRCCOPY = 13369376;
Class Functions#region Class Functions
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern bool BitBlt(
IntPtr hdcDest, //目的DC的句柄
int nXDest, //目的图形的左上角的x坐标
int nYDest, //目的图形的左上角的y坐标
int nWidth, //目的图形的矩形宽度
int nHeight, //目的图形的矩形高度
IntPtr hdcSrc, //源DC的句柄
int nXSrc, //源图形的左上角的x坐标
int nYSrc, //源图形的左上角的x坐标
System.Int32 dwRop //光栅操作代码
);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap
(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
#endregion
}
/**////
/// This class shall keep the User32 APIs used in our program.
///
public sealed class User32
...{
Class Variables#region Class Variables
public const int SM_CXSCREEN = 0;
public const int SM_CYSCREEN = 1;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
...{
public int left;
public int top;
public int right;
public int bottom;
}
#endregion
Class Functions#region Class Functions
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("user32.dll", EntryPoint = "GetWindowDC")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
#endregion
}
/**////
/// This class shall keep all the functionality for capturing
/// the desktop.
///
public class CaptureScreen
...{
Class Variable Declaration#region Class Variable Declaration
protected static IntPtr m_HBitmap;
#endregion
/**//// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public static Image CaptureDesktop()
...{
return CaptureWindow(User32.GetDesktopWindow());
}
/**//// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">
/// The handle to the window. (In windows forms, this is obtained by the Handle property)
/// </param>
/// <returns></returns>
public static 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;
}
/**//// <summary>
/// Captures a screen shot of a specific window, and saves it to a file
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public static void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
...{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
/**//// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public static void CaptureScreenToFile(string filename, ImageFormat format)
...{
Image img = CaptureDesktop();
img.Save(filename, format);
}
- c#使用API截屏类
- c#使用豆瓣API
- C#使用百度翻译API
- C# 网络API接口使用
- C#百度定位API使用
- C#中使用API函数ShellExecute
- C#中使用API来实现BEEP
- C#中通过DllImport使用 Win32 API
- C#中通过DllImport使用 Win32 API
- 使用 C# 和 Yahoo API 做天气预报
- C#中通过DllImport使用 Win32 API
- c#使用Api函数重设用户密码
- C#中通过DllImport使用 Win32 API
- C#中通过DllImport使用 Win32 API
- 有道词典的API使用 C#
- C#中windos API SendMessage的使用
- C#使用GDI中的API函数
- c# 使用api函数 ShowWindowAsync 控制窗体
- C# 窗体间调用
- 昨天的梦
- 感恩—父亲节、母亲节由来及日期
- 企业信息化咨询的钱哪去了[转]
- Steven
- c#使用API截屏类
- 用oracle中的序列替换sql server中top的方法
- 在csdn上好玩的话
- C++学习笔记(一)
- J2SE(TM) 5.0语言元素
- CodeSmith基础(一) http://www.cnblogs.com/Bear-Study-Hard/archive/2005/12/19/300112.html
- 第一次作业题目运用三大关键分析产品 Search.csdn.net
- 页面自动刷新
- 显示隐藏框架