C#做Screen Capture程序

开发 后端
这里介绍C#做Screen Capture程序,在掌握了一些C#源代码后,可以得到用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程序

【编辑推荐】

  1. 浅谈C# Connection对象
  2. C#实现PrintPage方法
  3. 利用Visual C#和C#语言特性
  4. C#管道技术学习经验
  5. 概述C#复合控件构建
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-20 10:54:29

C#做浏览器源程序

2009-08-20 16:02:15

C#正则表达式

2009-08-13 17:04:09

C#语言C#程序

2009-08-19 17:11:49

C#程序集

2009-08-20 17:49:53

学习C#程序

2009-08-07 17:32:17

C#编译程序

2009-08-12 18:28:09

C#事件处理程序

2009-08-12 17:44:30

C# Web Serv

2009-08-13 17:15:44

C#屏幕保护程序

2009-08-24 15:46:46

C# SmartPho

2009-08-11 13:48:11

C# ConfigDl

2009-08-24 09:25:18

Visual C# ..NET应用程序

2009-08-26 15:10:34

脱离.net fram

2009-08-28 16:03:15

C#程序实现鼠标移动

2009-08-25 17:24:55

C#串口通信程序

2009-08-24 14:19:27

C# Windows应

2009-08-06 10:27:08

C#应用程序域

2009-08-14 11:00:16

C#创建Windows

2009-08-12 18:20:39

C#事件驱动程序

2011-04-08 09:52:44

C++C#DLL
点赞
收藏

51CTO技术栈公众号