一、截取屏幕图像
在Windows中可以使用PrintScreen键进行截屏操作,将屏幕图像保存到剪贴板中,然后可以在其他应用程序,如“画图板”中进行处理,也可以直接粘贴到其他应用程序中。
新建一个Windows窗体应用程序,命名为CaptureScreen。新建一个类,命名为PlatFormInvokeGDI32.cs:
using System;
using System.Runtime.InteropServices;
namespace CaptureScreen
{
///<summary>
///引入一些GDI32中的方法
///</summary>
public class platformInvokeGDI32
{
#region Class Variables
public const int SRCCOPY = 13369376;
#endregion
#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,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);
[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
#region Public Constructor
public PlatformInvokeGDI32()
{
}
#endregion
}
}
新建一个类,命名为PlatformInvokeUSER32.cs
using System;
using System.Runtime.InteropServices;
namespace CaptureScreen
{
///<summary>
///引入一些User32中的方法
///</summary>
public class platformInvokeUSER32
{
#region Class Variables
public const int SM_CXSCREEN=0;
public const int SM_CYSCREEN=1;
#endregion
#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(Int32 ptr);
[DllImport("user32.dll",EntryPoint="ReleaseDC")]
public static extern Intptr ReleaseDC(Intptr hWnd,Intptr hDc);
#endregion
#region public Constructor
public PlatformInvokeUSER32()
{
}
#endregion
}
///<summary>
///表示屏幕大小
///</summary>
public struct SIZE
{
public int cx;
public int cy;
}
}
新建一个类,命名为CaptureScreen.cs
using System;
using System.Drawing;
namespace CaptureScreen
{
///<summary>
///CaptureScreen类
///</summary>
public class CaptureScreen
{
#region public class Functions
public static Bitmap GetDesktopImage()
{
//屏幕大小
SIZE size;
//图像
IntPtr hBitmap;
//User32中的GetDesktopWindow方法
IntPtr hDC=PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
//GDI32中的方法
Intptr hMemDC=PlatformInvokeGDI32.CreateCompatibleDC(hDC);
//宽度
size.cx =PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);
//长度
size.cy=PlatformInvokeUSE32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);
//创建图像
hBitmap =PlatformInvokeGDI32.CreateCompatibleBitmap(hDC,size.cx,size.cy);
//截屏处理
if(hBitmap !=IntPtr.Zero)
{
IntPtr hOld=(IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC,hBitmap);
PlatformInvokeGDI32.BitBlt(hMemDC,0,0,size.cx,size.cy,hDC,0,0,PlatformInvokeGDI32.SRCCOPY);
PlatformInvokeGDI32.SelectObject(hMemDC,hOld);
PlatformInvokeGDI32.DeleteDC(hMemDC);
PlatformInvokeGDI32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(),hDC);
Bitmap bmp=System.Drawing.Image.FromHbitmap(hBitmap);
PlatformInvokeGDI32.DeleteObject(hBitmap);
GC.Collect();
//返回图像
return bmp;
}
return null;
}
#endregion
}
}
截屏操作:
private void mnuCaptureScreen_Click(object sender,System.EventArgs e)
{
picScreen.Image=CaptureScreen.GetDesktopImage();
}
private void mnuExit_Click(object sender,System.EventArgs e)
{
Application.Exit();
}