screencapture(怎么用C#做Screen Capture程序)

发布时间:2025-12-11 01:28:15 浏览次数:1

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

  1. usingSystem;

  2. usingSystem.Drawing;

  3. usingSystem.Collections;

  4. usingSystem.ComponentModel;

  5. usingSystem.Windows.Forms;

  6. usingSystem.Data;

  7. usingSystem.Drawing.Imaging;

  8. usingSystem.IO;

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

  10. publicclassCapture:Form

  11. {

  12. privateSystem.ComponentModel.Containercomponents=null;

  13. privateIconmNetTrayIcon=newIcon("Tray.ico");

  14. privateBitmapMyImage=null;

  15. privateNotifyIconTrayIcon;

  16. privateContextMenunotifyiconMnu;

  17. publicCapture()

  18. {

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

  20. InitializeComponent();

  21. }

  22. protectedoverridevoidOnActivated(EventArgse)

  23. {

  24. this.Hide();

  25. }

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

  27. privatestaticexternboolBitBlt(

  28. IntPtrhdcDest,//目标设备的句柄

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

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

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

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

  33. IntPtrhdcSrc,//源设备的句柄

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

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

  36. System.Int32dwRop//光栅的操作值

  37. );

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

  39. privatestaticexternIntPtrCreateDC(

  40. stringlpszDriver,//驱动名称

  41. stringlpszDevice,//设备名称

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

  43. IntPtrlpInitData//任意的打印机数据

  44. );

  45. publicvoidcapture(objectsender,System.EventArgse)

  46. {

  47. this.Visible=false;

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

  49. //创建显示器的DC

  50. Graphicsg1=Graphics.FromHdc(dc1);

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

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

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

  54. Graphicsg2=Graphics.FromImage(MyImage);

  55. //获得屏幕的句柄

  56. IntPtrdc3=g1.GetHdc();

  57. //获得位图的句柄

  58. IntPtrdc2=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. publicvoidExitSelect(objectsender,System.EventArgse)

  71. {

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

  73. TrayIcon.Visible=false;

  74. //关闭系统

  75. this.Close();

  76. }

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

  78. publicoverridevoidDispose()

  79. {

  80. base.Dispose();

  81. if(components!=null)

  82. components.Dispose();

  83. }

  84. privatevoidInitializeComponent()

  85. {

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

  87. TrayIcon=newNotifyIcon();

  88. TrayIcon.Icon=mNetTrayIcon;

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

  90. TrayIcon.Visible=true;

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

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

  93. mnuItms[0]=newMenuItem();

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

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

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

  97. mnuItms[2]=newMenuItem();

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

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

  100. mnuItms[2].DefaultItem=true;

  101. notifyiconMnu=newContextMenu(mnuItms);

  102. TrayIcon.ContextMenu=notifyiconMnu;

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

  104. this.SuspendLayout();

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

  106. this.ClientSize=newSystem.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#做ScreenCapture程序!";

  114. this.ResumeLayout(false);

  115. }

  116. staticvoidMain()

  117. {

  118. Application.Run(newCapture());

  119. }

  120. }

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

screencapture
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477