怎么用C#做Screen Capture程序

发布时间:2021-12-02 11:33:29 作者:iii
来源:亿速云 阅读:131

这篇文章主要讲解了“怎么用C#做Screen Capture程序”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用C#做Screen Capture程序”吧!

在掌握了一些C#源代码后,可以得到用C#做Screen Capture程序的源代码(Capture.cs),具体如下:

  1. using System ;  

  2. using System.Drawing ;  

  3. using System.Collections ;  

  4. using System.ComponentModel ;  

  5. using System.Windows.Forms ;  

  6. using System.Data ;  

  7. using System.Drawing.Imaging ;  

  8. using System.IO ;  

  9. //导入在程序中使用到的名称空间  

  10. public class Capture : Form  

  11. {  

  12. private System.ComponentModel.Container components = null ;  

  13. private Icon mNetTrayIcon = new Icon ( "Tray.ico" ) ;  

  14. private Bitmap MyImage = null ;  

  15. private NotifyIcon TrayIcon ;  

  16. private ContextMenu notifyiconMnu ;  

  17. public Capture ( )  

  18. {  

  19. //初始化窗体中使用到的组件  

  20. InitializeComponent ( ) ;  

  21. }  

  22. protected override void OnActivated ( EventArgs e )  

  23. {  

  24. this.Hide ( ) ;  

  25. }  

  26. [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]  

  27. private static extern bool BitBlt (  

  28. IntPtr hdcDest , //目标设备的句柄  

  29. int nXDest , // 目标对象的左上角的X坐标  

  30. int nYDest , // 目标对象的左上角的X坐标  

  31. int nWidth , // 目标对象的矩形的宽度  

  32. int nHeight , // 目标对象的矩形的长度  

  33. IntPtr hdcSrc , // 源设备的句柄  

  34. int nXSrc , // 源对象的左上角的X坐标  

  35. int nYSrc , // 源对象的左上角的X坐标  

  36. System.Int32 dwRop // 光栅的操作值  

  37. ) ;  

  38. [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]  

  39. private static extern IntPtr CreateDC (  

  40. string lpszDriver , // 驱动名称  

  41. string lpszDevice , // 设备名称  

  42. string lpszOutput , // 无用,可以设定位"NULL"  

  43. IntPtr lpInitData // 任意的打印机数据  

  44. ) ;  

  45. public void capture ( object sender , System.EventArgs e )  

  46. {  

  47. this.Visible = false ;  

  48. IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;  

  49. //创建显示器的DC  

  50. Graphics g1 = Graphics.FromHdc ( dc1 ) ;  

  51. //由一个指定设备的句柄创建一个新的Graphics对象  

  52. MyImage = new Bitmap ( Screen.PrimaryScreen.Bounds.Width , 
    Screen.PrimaryScreen.Bounds.Height , g1 ) ;  

  53. //根据屏幕大小创建一个与之相同大小的Bitmap对象  

  54. Graphics g2 = Graphics.FromImage ( MyImage ) ;  

  55. //获得屏幕的句柄  

  56. IntPtr dc3 = g1.GetHdc ( ) ;  

  57. //获得位图的句柄  

  58. IntPtr dc2 = g2.GetHdc ( ) ;  

  59. //把当前屏幕捕获到位图对象中  

  60. BitBlt ( dc2 , 0 , 0 , Screen.PrimaryScreen.Bounds.Width , 
    Screen.PrimaryScreen.Bounds.Height , dc3 , 0 , 0 , 13369376 ) ;  

  61. //把当前屏幕拷贝到位图中  

  62. g1.ReleaseHdc ( dc3 ) ;  

  63. //释放屏幕句柄  

  64. g2.ReleaseHdc ( dc2 ) ;  

  65. //释放位图句柄  

  66. MyImage.Save ( "c:\\MyJpeg.jpg" , ImageFormat.Jpeg ) ;  

  67. MessageBox.Show ( "已经把当前屏幕保存到C:\\MyJpeg.jpg文件中!" ) ;  

  68. this.Visible = true ;  

  69. }  

  70. public void ExitSelect ( object sender , System.EventArgs e )  

  71. {  

  72. //隐藏托盘程序中的图标  

  73. TrayIcon.Visible = false ;  

  74. //关闭系统  

  75. this.Close ( ) ;  

  76. }  

  77. //清除程序中使用过的资源  

  78. public override void Dispose ( )  

  79. {  

  80. base.Dispose ( ) ;  

  81. if ( components != null )  

  82. components.Dispose ( ) ;  

  83. }  

  84. private void InitializeComponent ( )  

  85. {  

  86. //设定托盘程序的各个属性  

  87. TrayIcon = new NotifyIcon ( ) ;  

  88. TrayIcon.Icon = mNetTrayIcon ;  

  89. TrayIcon.Text = "用C#做Screen Capture程序" ;  

  90. TrayIcon.Visible = true ;  

  91. //定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象  

  92. MenuItem [ ] mnuItms = new MenuItem [ 3 ] ;  

  93. mnuItms [ 0 ] = new MenuItem ( ) ;  

  94. mnuItms [ 0 ] .Text = "捕获当前屏幕!" ;  

  95. mnuItms [ 0 ] .Click += new System.EventHandler ( this.capture ) ;  

  96. mnuItms [ 1 ] = new MenuItem ( "-" ) ;  

  97. mnuItms [ 2 ] = new MenuItem ( ) ;  

  98. mnuItms [ 2 ] .Text = "退出系统" ;  

  99. mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;  

  100. mnuItms [ 2 ] .DefaultItem = true ;  

  101. notifyiconMnu = new ContextMenu ( mnuItms ) ;  

  102. TrayIcon.ContextMenu = notifyiconMnu ;  

  103. //为托盘程序加入设定好的ContextMenu对象  

  104. this.SuspendLayout ( ) ;  

  105. this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;  

  106. this.ClientSize = new System.Drawing.Size ( 320 , 56 ) ;  

  107. this.ControlBox = false ;  

  108. this.MaximizeBox = false ;  

  109. this.MinimizeBox = false ;  

  110. this.WindowState = System.Windows.Forms.FormWindowState.Minimized ;  

  111. this.Name = "capture" ;  

  112. this.ShowInTaskbar = false ;  

  113. this.Text = "用C#做Screen Capture程序!" ;  

  114. this.ResumeLayout ( false ) ;  

  115. }  

  116. static void Main ( )  

  117. {  

  118. Application.Run ( new Capture ( ) ) ;  

  119. }  

  120. }  

感谢各位的阅读,以上就是“怎么用C#做Screen Capture程序”的内容了,经过本文的学习后,相信大家对怎么用C#做Screen Capture程序这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 简介QTP中的Active Screen
  2. C#中正则表达式匹配与替换字符串功能的示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:VB.NET对象原理的示例分析

下一篇:tk.Mybatis插入数据获取Id怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》