怎样使用C#进行窗口截图

 时间:2024-10-12 22:58:52

1、启动VS2013,新建C#->WinForm项目,命名为Cs窗口截图,如下图:

怎样使用C#进行窗口截图

3、添加Win32Api引用,源代码见参考资料。

4、添加PrtWnd类,用于截取给定句柄的窗口,代码:public class PrtWnd { public stat足毂忍珩ic Bitmap PrtWindow(IntPtr hWnd) { IntPtr hscrdc =Win32Api.GetWindowDC(hWnd); Win32Api.RECT rect; Win32Api.GetWindowRect(hWnd,out rect); IntPtr hbitmap =Win32Api.CreateCompatibleBitmap(hscrdc, rect.right-rect .left , rect .bottom -rect .top ); IntPtr hmemdc =Win32Api.CreateCompatibleDC(hscrdc); Win32Api.SelectObject(hmemdc, hbitmap); Win32Api. PrintWindow(hWnd, hmemdc, 0); Bitmap bmp = Bitmap.FromHbitmap(hbitmap); Win32Api .DeleteDC(hscrdc); Win32Api.DeleteDC(hmemdc); return bmp; } }

5、在Form1的Btton单击事件中添加代码:private void button1_Click(o芟鲠阻缒bject sender, EventArgs e) { IntPtr handle=IntPtr .Zero ; try { handle = Win32Api.FindWindow(null, textBox1.Text); PB.Image = PrtWnd.PrtWindow(handle); } catch (Exception) { MessageBox.Show("悲剧……可能哪里出错了。|"+handle .ToString()); } }

6、调试运行,结果如下图:

怎样使用C#进行窗口截图
  • lol端游如何利用elo机制上分
  • Fedora Linux怎么安装qq
  • 新版LOL荣誉系统怎么到达荣誉4级
  • 豆瓣怎么玩?
  • 轻松遍历出来文件夹下的所有文件
  • 热门搜索
    水光潋滟的意思 祝福老师的句子 莞尔的意思 开超市要注意什么 美国代购什么便宜 饯行是什么意思 61儿童节祝福语 scan是什么意思 花枝招展是什么意思 阳春白雪是什么意思