其中有个功能是需要动态捕捉屏幕上显示的内容发送到LED屏,
现成整理出了一个屏幕捕捉类。如下:
using
System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Drawing;
namespace wuChang
{
/// <summary>
/// 屏幕捕捉类
/// 无常作品
/// msn:wuChangx@hotmail.com
/// QQ: 3263262
/// http://www.cnblogs.com/wuchang
/// </summary>
internal class ScreenCapture : IDisposable
{
private static ScreenCapture instance = null ;
internal static ScreenCapture Instance
{
get
{
if (instance == null )
{
instance = new ScreenCapture();
}
return ScreenCapture.instance;
}
}
int hdcSrc,
hdcDest;
private ScreenCapture()
{
hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow());
hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
}
/// <summary>
/// 屏幕捕捉
/// </summary>
/// <param name="rct"> 要捕捉的桌面区域 </param>
/// <returns> 捕获后的图形 </returns>
internal Bitmap Capture(Rectangle rct)
{
int hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, rct.Width, rct.Height);
GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0 , 0 , rct.Width, rct.Height,
hdcSrc, rct.Left, rct.Top, 0x00CC0020 );
Bitmap image = new Bitmap(Image.FromHbitmap( new IntPtr(hBitmap)),
Image.FromHbitmap( new IntPtr(hBitmap)).Width,
Image.FromHbitmap( new IntPtr(hBitmap)).Height);
GDI32.DeleteObject(hBitmap);
return image;
}
#region IDisposable Members
public void Dispose()
{
User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc);
GDI32.DeleteDC(hdcDest);
}
#endregion
}
//下面二个类来自:http://www.c-sharpcorner.com/Code/2003/Dec/ScreenCapture.asp
class GDI32
{
[DllImport( " GDI32.dll " )]
public static extern bool BitBlt( int hdcDest, int nXDest, int nYDest,
int nWidth, int nHeight, int hdcSrc,
int nXSrc, int nYSrc, int dwRop);
[DllImport( " GDI32.dll " )]
public static extern int CreateCompatibleBitmap( int hdc, int nWidth,
int nHeight);
[DllImport( " GDI32.dll " )]
public static extern int CreateCompatibleDC( int hdc);
[DllImport( " GDI32.dll " )]
public static extern bool DeleteDC( int hdc);
[DllImport( " GDI32.dll " )]
public static extern bool DeleteObject( int hObject);
[DllImport( " GDI32.dll " )]
public static extern int GetDeviceCaps( int hdc, int nIndex);
[DllImport( " GDI32.dll " )]
public static extern int SelectObject( int hdc, int hgdiobj);
}
class User32
{
[DllImport( " User32.dll " )]
public static extern int GetDesktopWindow();
[DllImport( " User32.dll " )]
public static extern int GetWindowDC( int hWnd);
[DllImport( " User32.dll " )]
public static extern int ReleaseDC( int hWnd, int hDC);
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Drawing;
namespace wuChang
{
/// <summary>
/// 屏幕捕捉类
/// 无常作品
/// msn:wuChangx@hotmail.com
/// QQ: 3263262
/// http://www.cnblogs.com/wuchang
/// </summary>
internal class ScreenCapture : IDisposable
{
private static ScreenCapture instance = null ;
internal static ScreenCapture Instance
{
get
{
if (instance == null )
{
instance = new ScreenCapture();
}
return ScreenCapture.instance;
}
}
int hdcSrc,
hdcDest;
private ScreenCapture()
{
hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow());
hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
}
/// <summary>
/// 屏幕捕捉
/// </summary>
/// <param name="rct"> 要捕捉的桌面区域 </param>
/// <returns> 捕获后的图形 </returns>
internal Bitmap Capture(Rectangle rct)
{
int hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, rct.Width, rct.Height);
GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0 , 0 , rct.Width, rct.Height,
hdcSrc, rct.Left, rct.Top, 0x00CC0020 );
Bitmap image = new Bitmap(Image.FromHbitmap( new IntPtr(hBitmap)),
Image.FromHbitmap( new IntPtr(hBitmap)).Width,
Image.FromHbitmap( new IntPtr(hBitmap)).Height);
GDI32.DeleteObject(hBitmap);
return image;
}
#region IDisposable Members
public void Dispose()
{
User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc);
GDI32.DeleteDC(hdcDest);
}
#endregion
}
//下面二个类来自:http://www.c-sharpcorner.com/Code/2003/Dec/ScreenCapture.asp
class GDI32
{
[DllImport( " GDI32.dll " )]
public static extern bool BitBlt( int hdcDest, int nXDest, int nYDest,
int nWidth, int nHeight, int hdcSrc,
int nXSrc, int nYSrc, int dwRop);
[DllImport( " GDI32.dll " )]
public static extern int CreateCompatibleBitmap( int hdc, int nWidth,
int nHeight);
[DllImport( " GDI32.dll " )]
public static extern int CreateCompatibleDC( int hdc);
[DllImport( " GDI32.dll " )]
public static extern bool DeleteDC( int hdc);
[DllImport( " GDI32.dll " )]
public static extern bool DeleteObject( int hObject);
[DllImport( " GDI32.dll " )]
public static extern int GetDeviceCaps( int hdc, int nIndex);
[DllImport( " GDI32.dll " )]
public static extern int SelectObject( int hdc, int hgdiobj);
}
class User32
{
[DllImport( " User32.dll " )]
public static extern int GetDesktopWindow();
[DllImport( " User32.dll " )]
public static extern int GetWindowDC( int hWnd);
[DllImport( " User32.dll " )]
public static extern int ReleaseDC( int hWnd, int hDC);
}
}
使用方法:
1、捕捉主显示器的全屏内容
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
wuChang.ScreenCapture.Instance.Capture(
this
.Bounds ).Save(
@"
c:\aa.bmp
"
, ImageFormat.Bmp);