欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 使用BitBlt進行窗口抓圖的示例

 更新時間:2021年01月07日 09:21:56   作者:xhubobo  
這篇文章主要介紹了C# 使用BitBlt進行窗口抓圖的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

本文和C++使用BitBlt進行窗口抓圖對應(yīng),使用C#實現(xiàn)。

這種方式對1920*1080大小的窗口,一次抓圖的時間參考(VS2015+i5 9400F):低至2~3ms(平均4.3ms)。

參見:C#抓圖服務(wù)。

1、Win32封裝

Win32Consts

using System.ComponentModel;

namespace CaptureSharp
{
 public sealed class Win32Consts
 {
 public enum DibColorMode : uint
 {
  DIB_RGB_COLORS = 0x00,
  DIB_PAL_COLORS = 0x01,
  DIB_PAL_INDICES = 0x02
 }

 public enum BitmapCompressionMode : uint
 {
  BI_RGB = 0,
  BI_RLE8 = 1,
  BI_RLE4 = 2,
  BI_BITFIELDS = 3,
  BI_JPEG = 4,
  BI_PNG = 5
 }

 public enum RasterOperationMode : uint
 {
  SRCCOPY = 0x00CC0020,
  SRCPAINT = 0x00EE0086,
  SRCAND = 0x008800C6,
  SRCINVERT = 0x00660046,
  SRCERASE = 0x00440328,
  NOTSRCCOPY = 0x00330008,
  NOTSRCERASE = 0x001100A6,
  MERGECOPY = 0x00C000CA,
  MERGEPAINT = 0x00BB0226,
  PATCOPY = 0x00F00021,
  PATPAINT = 0x00FB0A09,
  PATINVERT = 0x005A0049,
  DSTINVERT = 0x00550009,
  BLACKNESS = 0x00000042,
  WHITENESS = 0x00FF0062,
  CAPTUREBLT = 0x40000000 //only if WinVer >= 5.0.0 (see wingdi.h)
 }

 public enum PrintWindowMode : uint
 {
  [Description(
  "Only the client area of the window is copied to hdcBlt. By default, the entire window is copied.")]
  PW_CLIENTONLY = 0x00000001,

  [Description("works on windows that use DirectX or DirectComposition")]
  PW_RENDERFULLCONTENT = 0x00000002
 }
 }
}

Win32Types

using System.Runtime.InteropServices;

namespace CaptureSharp
{
 public sealed class Win32Types
 {
 [StructLayout(LayoutKind.Sequential)]
 public struct Point
 {
  public int x;
  public int y;

  public Point(int x, int y)
  {
  this.x = x;
  this.y = y;
  }
 }

 [StructLayout(LayoutKind.Sequential)]
 public struct Rect
 {
  public int Left; //最左坐標
  public int Top; //最上坐標
  public int Right; //最右坐標
  public int Bottom; //最下坐標

  public int Width => Right - Left;
  public int Height => Bottom - Top;
 }

 [StructLayout(LayoutKind.Sequential, Pack = 2)]
 public struct BitmapFileHeader
 {
  public ushort bfType;
  public uint bfSize;
  public ushort bfReserved1;
  public ushort bfReserved2;
  public uint bfOffBits;
 }

 [StructLayout(LayoutKind.Sequential)]
 public struct BitmapInfoHeader
 {
  public uint biSize;
  public int biWidth;
  public int biHeight;
  public ushort biPlanes;
  public ushort biBitCount;
  public uint biCompression;
  public uint biSizeImage;
  public int biXPelsPerMeter;
  public int biYPelsPerMeter;
  public uint biClrUsed;
  public uint biClrImportant;

  public void Init()
  {
  biSize = (uint)Marshal.SizeOf(this);
  }
 }

 [StructLayout(LayoutKind.Sequential, Pack = 1)]
 public struct RgbQuad
 {
  public byte rgbBlue;
  public byte rgbGreen;
  public byte rgbRed;
  public byte rgbReserved;
 }

 [StructLayout(LayoutKind.Sequential, Pack = 1)]
 public struct BitmapInfo
 {
  public BitmapInfoHeader bmiHeader;
  public RgbQuad bmiColors;
 }
 }
}

Win32Funcs

using System;
using System.Runtime.InteropServices;

namespace CaptureSharp
{
 public sealed class Win32Funcs
 {
 [DllImport("User32.dll", SetLastError = true)]
 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool GetWindowRect(IntPtr hWnd, out Win32Types.Rect lpRect);

 [DllImport("user32.dll")]
 public static extern bool GetClientRect(IntPtr hWnd, out Win32Types.Rect lpRect);

 [DllImport("user32.dll", EntryPoint = "GetWindowDC")]
 public static extern IntPtr GetWindowDC(IntPtr hWnd);

 [DllImport("gdi32.dll")]
 public static extern IntPtr CreateCompatibleDC(IntPtr hDc);

 [DllImport("gdi32.dll")]
 public static extern IntPtr CreateCompatibleBitmap(IntPtr hDc, int nWidth, int nHeight);

 [DllImport("gdi32.dll")]
 public static extern bool DeleteDC(IntPtr hDc);

 [DllImport("user32.dll")]
 public static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

 [DllImport("gdi32.dll")]
 public static extern IntPtr CreateDIBSection(IntPtr hdc, ref Win32Types.BitmapInfo bmi,
  uint usage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);

 [DllImport("gdi32.dll")]
 public static extern IntPtr SelectObject(IntPtr hDc, IntPtr hObject);

 [DllImport("gdi32.dll")]
 public static extern bool DeleteObject(IntPtr hObject);

 [DllImport("gdi32.dll", SetLastError = true)]
 public static extern bool BitBlt(
  IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight,
  IntPtr hObjectSource, int nXSrc, int nYSrc, uint dwRop);

 [DllImport("user32.dll")]
 public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
 }
}

2、DibCaptureHelper.cs

using System;

namespace CaptureSharp
{
 internal class DibCaptureHelper
 {
 public IntPtr BitmapPtr => _hBitmap;
 public Win32Types.BitmapInfo BitmapInfo => _bitmapInfo;
 public Win32Types.Rect WindowRect => _windowRect;
 public Win32Types.Rect ClientRect => _clientRect;
 public int BitmapDataSize => _bmpDataSize;

 private IntPtr _hWnd = IntPtr.Zero;
 private IntPtr _hScrDc = IntPtr.Zero;
 private IntPtr _hMemDc = IntPtr.Zero;
 private IntPtr _hBitmap = IntPtr.Zero;
 private IntPtr _hOldBitmap = IntPtr.Zero;
 private IntPtr _bitsPtr = IntPtr.Zero;

 private Win32Types.BitmapInfo _bitmapInfo;
 private Win32Types.Rect _windowRect;
 private Win32Types.Rect _clientRect;
 private int _bmpDataSize;

 public bool Init(string windowName)
 {
  var handle = Win32Funcs.FindWindow(null, windowName);
  if (handle.Equals(IntPtr.Zero))
  {
  return false;
  }

  return Init(handle);
 }

 public bool Init(IntPtr handle)
 {
  _hWnd = handle;

  //獲取窗口大小
  if (!Win32Funcs.GetWindowRect(_hWnd, out _windowRect)
  || !Win32Funcs.GetClientRect(_hWnd, out _clientRect))
  {
  return false;
  }

  _bmpDataSize = _clientRect.Width * _clientRect.Height * 3;

  //位圖信息
  _bitmapInfo = new Win32Types.BitmapInfo {bmiHeader = new Win32Types.BitmapInfoHeader()};
  _bitmapInfo.bmiHeader.Init();
  _bitmapInfo.bmiHeader.biWidth = _clientRect.Width;
  _bitmapInfo.bmiHeader.biHeight = _clientRect.Height;
  _bitmapInfo.bmiHeader.biPlanes = 1;
  _bitmapInfo.bmiHeader.biBitCount = 24;
  _bitmapInfo.bmiHeader.biSizeImage = (uint) (_clientRect.Width * _clientRect.Height);
  _bitmapInfo.bmiHeader.biCompression = (uint) Win32Consts.BitmapCompressionMode.BI_RGB;

  _hScrDc = Win32Funcs.GetWindowDC(_hWnd);
  _hMemDc = Win32Funcs.CreateCompatibleDC(_hScrDc);
  _hBitmap = Win32Funcs.CreateDIBSection(_hMemDc, ref _bitmapInfo,
  (uint) Win32Consts.DibColorMode.DIB_RGB_COLORS,
  out _bitsPtr, IntPtr.Zero, 0);
  _hOldBitmap = Win32Funcs.SelectObject(_hMemDc, _hBitmap);

  return true;
 }

 public void Cleanup()
 {
  if (_hBitmap.Equals(IntPtr.Zero))
  {
  return;
  }

  //刪除用過的對象
  Win32Funcs.SelectObject(_hMemDc, _hOldBitmap);
  Win32Funcs.DeleteObject(_hBitmap);
  Win32Funcs.DeleteDC(_hMemDc);
  Win32Funcs.ReleaseDC(_hWnd, _hScrDc);

  _hWnd = IntPtr.Zero;
  _hScrDc = IntPtr.Zero;
  _hMemDc = IntPtr.Zero;
  _hBitmap = IntPtr.Zero;
  _hOldBitmap = IntPtr.Zero;
  _bitsPtr = IntPtr.Zero;
 }

 public bool RefreshWindow()
 {
  var hWnd = _hWnd;
  Cleanup();
  return Init(hWnd);
 }

 public bool ChangeWindowHandle(string windowName)
 {
  Cleanup();
  return Init(windowName);
 }

 public bool ChangeWindowHandle(IntPtr handle)
 {
  Cleanup();
  return Init(handle);
 }

 public IntPtr Capture()
 {
  if (_hBitmap.Equals(IntPtr.Zero) || _hMemDc.Equals(IntPtr.Zero) || _hScrDc.Equals(IntPtr.Zero))
  {
  return IntPtr.Zero;
  }

  var ret = Win32Funcs.BitBlt(
  _hMemDc, 0, 0, _clientRect.Width, _clientRect.Height,
  _hScrDc, 0, 0,
  (uint) Win32Consts.RasterOperationMode.SRCCOPY);

  return ret ? _bitsPtr : IntPtr.Zero;
 }

 public bool Capture(out IntPtr bitsPtr, out int bufferSize, out Win32Types.Rect rect)
 {
  bitsPtr = _bitsPtr;
  bufferSize = _bmpDataSize;
  rect = _clientRect;

  if (_hBitmap.Equals(IntPtr.Zero) || _hMemDc.Equals(IntPtr.Zero) || _hScrDc.Equals(IntPtr.Zero))
  {
  return false;
  }

  var ret = Win32Funcs.BitBlt(
  _hMemDc, 0, 0, _clientRect.Width, _clientRect.Height,
  _hScrDc, 0, 0,
  (uint) Win32Consts.RasterOperationMode.SRCCOPY);

  return ret;
 }
 }
}

以上就是C# 使用BitBlt進行窗口抓圖的示例的詳細內(nèi)容,更多關(guān)于c# 窗口抓圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c#中SqlHelper封裝SqlDataReader的方法

    c#中SqlHelper封裝SqlDataReader的方法

    這篇文章主要介紹了c#中SqlHelper封裝SqlDataReader的方法,涉及C#針對數(shù)據(jù)庫相關(guān)操作封裝與使用的技巧,需要的朋友可以參考下
    2015-05-05
  • C#中Invoke的具體使用

    C#中Invoke的具體使用

    本文主要介紹了C#中Invoke的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2023-08-08
  • 舊項目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題

    舊項目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題

    在項目開發(fā)過程中,不可避免的會升級開發(fā)工具。這次我在舊項目版本升級到新版Unity2021.2.x時,出現(xiàn)Visual?Studio無法定位等問題,這里我給大家分享下解決方法,舊項目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題,需要的朋友可以參考下
    2021-12-12
  • c#的sortedlist使用方法

    c#的sortedlist使用方法

    這篇文章主要介紹了c#的sortedlist使用方法,需要的朋友可以參考下
    2014-05-05
  • C#使用ImitateLogin模擬登錄百度

    C#使用ImitateLogin模擬登錄百度

    這篇文章主要介紹了C#使用ImitateLogin模擬登錄百度 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • VS2010中l(wèi)ib與dll文件的生成與使用方法

    VS2010中l(wèi)ib與dll文件的生成與使用方法

    這篇文章主要介紹了VS2010中l(wèi)ib與dll文件的生成與使用方法,需要的朋友可以參考下
    2018-01-01
  • C#操作JSON(序列化與反序列化)的方法詳解

    C#操作JSON(序列化與反序列化)的方法詳解

    .net?core提供了Json處理模塊,在命名空間System.Text.Json中,本文將通過頂級語句,對C#的Json功能進行講解,感興趣的小伙伴可以了解一下
    2023-05-05
  • C#調(diào)用FFplay實現(xiàn)播放視頻功能

    C#調(diào)用FFplay實現(xiàn)播放視頻功能

    這篇文章主要為大家詳細介紹了C#如何調(diào)用FFplay實現(xiàn)播放視頻功能,文中的示例代碼講解詳細,具有一定的參考價值,有需要的小伙伴可以跟隨小編一起學(xué)習一下
    2023-10-10
  • 讀寫XML文件的內(nèi)容并將其顯示在ListView控件上的方法

    讀寫XML文件的內(nèi)容并將其顯示在ListView控件上的方法

    下面小編就為大家?guī)硪黄x寫XML文件的內(nèi)容并將其顯示在ListView控件上的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • C#實現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類實例

    C#實現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類實例

    這篇文章主要介紹了C#實現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類,結(jié)合實例形式分析了C#針對日期的轉(zhuǎn)換與計算相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06

最新評論