其中有個功能是需要動態捕捉螢幕上顯示的內容傳送到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、捕捉主顯示器的全屏內容
wuChang.ScreenCapture.Instance.Capture(Screen.PrimaryScreen.Bounds).Save(@"c:\aa.bmp", ImageFormat.Bmp);
2、捕捉當前視窗內容wuChang.ScreenCapture.Instance.Capture( this.Bounds ).Save(@"c:\aa.bmp", ImageFormat.Bmp);