用截取窗口图像的方法,获得指定窗口的图像信息,但当窗口被挡住时,截取错误。以下是代码,
如果窗口没有被挡住,截取正确,被挡住就错误了,求高手指教! Graphics g1 = pictureBox1.CreateGraphics();
int iWidth = pictureBox1.Width;
int iHeight = pictureBox1.Height; Image MyImage = new Bitmap(pictureBox1.Width, pictureBox1.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
//得到屏幕的DC
IntPtr dc1 = g1.GetHdc();
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc(); BitBlt(dc2, 0, 0, pictureBox1.Width, pictureBox1.Height, dc1, 0, 0, 13369376); //释放掉屏幕的DC
g1.ReleaseHdc(dc1);
//释放掉Bitmap的DC
g2.ReleaseHdc(dc2); //MyImage.Save(@"d:\Capture.jpg"/*, ImageFormat.Jpeg*/);
Clipboard.SetImage(MyImage);
如果窗口没有被挡住,截取正确,被挡住就错误了,求高手指教! Graphics g1 = pictureBox1.CreateGraphics();
int iWidth = pictureBox1.Width;
int iHeight = pictureBox1.Height; Image MyImage = new Bitmap(pictureBox1.Width, pictureBox1.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
//得到屏幕的DC
IntPtr dc1 = g1.GetHdc();
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc(); BitBlt(dc2, 0, 0, pictureBox1.Width, pictureBox1.Height, dc1, 0, 0, 13369376); //释放掉屏幕的DC
g1.ReleaseHdc(dc1);
//释放掉Bitmap的DC
g2.ReleaseHdc(dc2); //MyImage.Save(@"d:\Capture.jpg"/*, ImageFormat.Jpeg*/);
Clipboard.SetImage(MyImage);
public static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}