设置了TransparencyKey或Opacity属性的表单是所谓的分层窗口。使用视频适配器的“覆盖”功能显示它们。这使他们能够具有透明效果。
捕获它们需要在CopyFromScreen重载中打开CopyPixelOperation.CaptureBlt选项,该重载接受CopyPixelOperation参数。
不幸的是,此重载有一个严重的错误,阻止了它的正常运行。它无法正确验证该值。仍未在.NET 4.0中修复。没有其他好的修复方法,只能使用P /
Invoke进行屏幕截图。这是一个例子:
using System;using System.Drawing;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Size sz = Screen.PrimaryScreen.Bounds.Size; IntPtr hDesk = GetDesktopWindow(); IntPtr hSrce = GetWindowDC(hDesk); IntPtr hDest = CreateCompatibleDC(hSrce); IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height); IntPtr hOldBmp = SelectObject(hDest, hBmp); bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Bitmap bmp = Bitmap.FromHbitmap(hBmp); SelectObject(hDest, hOldBmp); DeleteObject(hBmp); DeleteDC(hDest); ReleaseDC(hDesk, hSrce); bmp.Save(@"c:temptest.png"); bmp.Dispose(); } // P/Invoke declarations [Dllimport("gdi32.dll")] static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop); [Dllimport("user32.dll")] static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc); [Dllimport("gdi32.dll")] static extern IntPtr DeleteDC(IntPtr hDc); [Dllimport("gdi32.dll")] static extern IntPtr DeleteObject(IntPtr hDc); [Dllimport("gdi32.dll")] static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [Dllimport("gdi32.dll")] static extern IntPtr CreateCompatibleDC(IntPtr hdc); [Dllimport("gdi32.dll")] static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); [Dllimport("user32.dll")] public static extern IntPtr GetDesktopWindow(); [Dllimport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr ptr); }}
Fwiw,Windows的更高版本提供了针对此错误的解决方法。不确定,我认为是Win7 SP1。如果 仅
传递CopyPixelOperation.CaptureBlt选项,则BitBlt()函数现在将执行所需的 *** 作。但是,当然,该解决方法并未追溯应用到早期Windows版本,因此您不能真正依赖它。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)