コード例 #1
0
  /// スクリーンキャプチャした結果をbyte[]に格納する
  /// @param[out] dpiX X軸方向のDPI
  /// @param[out] dpiY Y軸方向のDPI
  /// @return スクリーンキャプチャした結果のbyte[]
  public byte[] ExecuteByGetDIBits(out int dpiX, out int dpiY) {
    // Windowチェック
    if (!Common.Utilities.IsWindowValid(this.Window)) {
      dpiX = 96;
      dpiY = 96;
      return null;
    }

    // 結果を格納するためのbyte配列
    var result = new byte[this.Size];
    var bitmapInfo = this.BitmapInfo;

    // BitBlt
    var window = this.Window;
    var windowDC = User32.GetDC(window);
    dpiX = GDI32.GetDeviceCaps(windowDC, GDI32.LOGPIXELSX);
    dpiY = GDI32.GetDeviceCaps(windowDC, GDI32.LOGPIXELSY);
    var capturedDC = GDI32.CreateCompatibleDC(windowDC);
    var capturedBitmap = GDI32.CreateCompatibleBitmap(windowDC,
        this.ClippingWidth, this.ClippingHeight);
    {
      var originalBitmap = GDI32.SelectObject(capturedDC, capturedBitmap);
      GDI32.BitBlt(capturedDC,
                   0, 0, this.ClippingWidth, this.ClippingHeight,
                   windowDC,
                   this.ClippingX, this.ClippingY,
                   this.ShowLayeredWindow ? GDI32.SRCCOPY | GDI32.CAPTUREBLT
                                          : GDI32.SRCCOPY);
      GDI32.GetDIBits(capturedDC, capturedBitmap, 0, (uint)this.ClippingHeight, result,
                      ref bitmapInfo, GDI32.DIB_RGB_COLORS);
      GDI32.SelectObject(capturedDC, originalBitmap);
    }
    GDI32.DeleteObject(capturedBitmap);
    GDI32.DeleteDC(capturedDC);
    User32.ReleaseDC(window, windowDC);
    
    /// @todo(me) マウスカーソルの合成・・・?いるか?
    if (this.ShowCursor) {
      // nop
    }
    
    return result;
  }