截取具有视觉风格的程序的屏幕截图

时间:2015-06-15 03:18:23

标签: c#

我正在使用此代码

public static Bitmap PrintWindow(IntPtr hwnd)
{
      RECT rc;
      GetWindowRect(hwnd, out rc);

      Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format24bppRgb);
      Graphics gfxBmp = Graphics.FromImage(bmp);
      IntPtr hdcBitmap = gfxBmp.GetHdc();

      PrintWindow(hwnd, hdcBitmap, 0);

      gfxBmp.ReleaseHdc(hdcBitmap);
      gfxBmp.Dispose();

      return bmp;
 }

为了捕获程序的屏幕截图,它运行良好,但它只采用基本的视觉风格(请检查下面的图像,左边是上面代码捕获的,右边是Alt+Prntscr捕获的)

lRr1L.png

那么..无论如何都要捕获具有视觉风格的程序截图?

1 个答案:

答案 0 :(得分:1)

以下是您可以尝试的一些链接: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.drawtobitmap.aspx http://www.pinvoke.net/default.aspx/user32.printwindow

我使用的是:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;

...

private void CaptureWindow()
{
    Graphics mygraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);
    IntPtr dc1 = mygraphics.GetHdc();
    IntPtr dc2 = memoryGraphics.GetHdc();
    //BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 0x00CC0020);
    mygraphics.ReleaseHdc(dc1);
    memoryGraphics.ReleaseHdc(dc2);
}

其中memoryImage是要打印的位图。