C# 截取屏幕

Win32Api

public enum TernaryRasterOperations
{
    SRCCOPY = 0x00CC0020, /* dest = source*/
    SRCPAINT = 0x00EE0086, /* dest = source OR dest*/
    SRCAND = 0x008800C6, /* dest = source AND dest*/
    SRCINVERT = 0x00660046, /* dest = source XOR dest*/
    SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/
    NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/
    NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
    MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/
    MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/
    PATCOPY = 0x00F00021, /* dest = pattern*/
    PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/
    PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/
    DSTINVERT = 0x00550009, /* dest = (NOT dest)*/
    BLACKNESS = 0x00000042, /* dest = BLACK*/
    WHITENESS = 0x00FF0062, /* dest = WHITE*/
};
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
 IntPtr hdc,     // handle to DC
 int nWidth,     // width of bitmap, in pixels
 int nHeight     // height of bitmap, in pixels
 );
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
    IntPtr hdc   // handle to DC
 );
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
 IntPtr hdc,      // handle to DC
 IntPtr hgdiobj   // handle to object
 );
[DllImport("gdi32.dll")]
public static extern int DeleteDC(
 IntPtr hdc       // handle to DC
 );
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(string driver, string device, IntPtr res1, IntPtr res2);
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
    int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
[DllImport("user32.dll")]
public static extern bool PrintWindow(
 IntPtr hwnd,     // Window to copy,Handle to the window that will be copied. 
 IntPtr hdcBlt,   // HDC to print into,Handle to the device context. 
 UInt32 nFlags    // Optional flags,Specifies the drawing options. It can be one of the following values. 
 );
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);

截取方法

/// <summary>
/// 截取全屏
/// </summary>
public static Bitmap CaptureFullScreen()
{
    try
    {
        Rectangle rect = new Rectangle();
        rect.Width = Screen.PrimaryScreen.Bounds.Width;
        rect.Height = Screen.PrimaryScreen.Bounds.Height;
        IntPtr dcTmp = CreateDC("DISPLAY", "DISPLAY", (IntPtr)null, (IntPtr)null);
        Graphics gScreen = Graphics.FromHdc(dcTmp);
        Bitmap image = new Bitmap((int)(rect.Width), (int)(rect.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        Graphics gImage = Graphics.FromImage(image);
        IntPtr dcImage = gImage.GetHdc();
        IntPtr dcScreen = gScreen.GetHdc();
        BitBlt(dcImage, 0, 0, (int)(rect.Width), (int)(rect.Height), dcScreen, (int)(rect.Left), (int)(rect.Top), TernaryRasterOperations.SRCCOPY);
        gScreen.ReleaseHdc(dcScreen);
        gImage.ReleaseHdc(dcImage);
        return image;
    }
    catch (Exception ex)
    {
        return null;
    }
}
/// <summary>
/// 截取窗口
/// </summary>
/// <param name="windows">windows窗口</param>
/// <param name="saveFullPath">保存路径</param>
/// <returns></returns>
public static Bitmap CaptureWindow(Window windows)
{
    IntPtr myWindows = new WindowInteropHelper(windows).Handle;
    try
    {
        IntPtr hscrdc = GetWindowDC(myWindows);
        IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, Convert.ToInt32(windows.Width), Convert.ToInt32(windows.Height));
        IntPtr hmemdc = CreateCompatibleDC(hscrdc);
        SelectObject(hmemdc, hbitmap);
        PrintWindow(myWindows, hmemdc, 0);
        Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
        DeleteDC(hscrdc);
        DeleteDC(hmemdc);
        //(必须为jpeg,以保证可以设置author)
        return bmp;
    }
    catch (Exception ex)
    {
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值