使用视觉样式截取程序屏幕截图

3
我正在使用这段代码。
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”获取的截图)。
那么...有没有办法捕获带有视觉风格的程序截图呢?

这两张图片重叠在一起,即使如此,也不能展示你所要求的内容。 - SteveFerg
像素格式错误,可能是这个原因。请更改为Format32bppArgb并重试。 - Hans Passant
1
我自己找到了解决方法...通过改变PrintWindow方法中的第三个参数,我可以截取带有视觉样式的程序屏幕截图。 0 -> 基本视觉样式 1 -> 仅内容,无边框 2 -> 视觉样式 - GarretLR
1个回答

1
以下是您可以尝试的一些链接: https://msdn.microsoft.com/zh-cn/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是要打印的位图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接