C#关于PrintWindow函数截图的问题

qq_34624212 2016-12-03 09:09:25
代码如下,问题是:
1、我想截其他窗口的图,我试验过截图,截战网客户端没问题,但是截一个安卓模拟器的图却一直是黑屏,
2、还有就是我想实现隐藏窗口(不是最小化)的截图,然后也是黑屏,我又尝试把窗口放到屏幕外面截图,虽然能够截到图,但是截到的图却有点问题,虽然是全部窗口,但是却把窗口整个缩小放到整个图片的左上角
3、能不能有方法截最小化窗口的图

public static Bitmap GetWindowCapture(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd); //返回hWnd参数所指定的窗口的设备环境。
var windowRect = Rectangle.Empty;
GetWindowRect(hWnd, ref windowRect);
int width = windowRect.Width;
int height = windowRect.Height;
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);//该函数创建与指定的设备环境相关的设备兼容的位图
IntPtr hmemdc = CreateCompatibleDC(hscrdc);//该函数创建一个与指定设备兼容的内存设备上下文环境(DC)
SelectObject(hmemdc, hbitmap);//该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象
PrintWindow(hWnd, hmemdc,0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
//Clipboard.SetImage(bmp);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; //最左坐标
public int Top; //最上坐标
public int Right; //最右坐标
public int Bottom; //最下坐标
}



IntPtr a = FindWindow(null, "战网");


private void button1_Click(object sender, EventArgs e)
{


StringBuilder title = new StringBuilder(256);
//RECT rect = new RECT();
//GetWindowRect(a, ref rect);
//int width = rect.Right - rect.Left; //窗口的宽度
//int height = rect.Bottom - rect.Top; //窗口的高度
//GetWindowText(a, title, title.Capacity);
this.textBox1.Text = title.ToString();
this.textBox2.Text = a.ToString();
//MoveWindow(a, 800, 800, width, height, true);//移动窗口到屏幕外

Image img = GetWindowCapture(a);
pictureBox1.Image = img;
//img.Save("D:\\Test.jpg");
// img.Dispose();




}

}
}
}
...全文
567 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
crystal_lz 2016-12-05
  • 打赏
  • 举报
回复
不是所有图都能截取的 比如一些游戏或者播放器 他们的渲染方式不一样 而你的那种截图方式是基于GDI的
qq_34624212 2016-12-04
  • 打赏
  • 举报
回复
大神们在哪里啊 有没有能来帮帮我的

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧