首页 > 编程语言 >基于C#实现屏幕桌面截图

基于C#实现屏幕桌面截图

时间:2022-12-30 21:35:00浏览次数:70  
标签:截图 桌面 C# System Windows button1 Form1 pictureBox1 new

原文网址:https://www.jb51.net/article/269794.htm

代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]         private static extern int GetSystemMetrics(int mVal);         private void button1_Click(object sender, EventArgs e)         {             Bitmap _Source = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));             using (Graphics g = Graphics.FromImage(_Source))             {                 g.CopyFromScreen(0, 0, 0, 0, _Source.Size);                 g.Dispose();             }             pictureBox1.Image = _Source;         }     }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 partial class Form1 {     /// <summary>     /// 必需的设计器变量。     /// </summary>     private System.ComponentModel.IContainer components = null;       /// <summary>     /// 清理所有正在使用的资源。     /// </summary>     /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>     protected override void Dispose(bool disposing)     {         if (disposing && (components != null))         {             components.Dispose();         }         base.Dispose(disposing);     }       #region Windows 窗体设计器生成的代码       /// <summary>     /// 设计器支持所需的方法 - 不要     /// 使用代码编辑器修改此方法的内容。     /// </summary>     private void InitializeComponent()     {         this.button1 = new System.Windows.Forms.Button();         this.pictureBox1 = new System.Windows.Forms.PictureBox();         ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();         this.SuspendLayout();         //         // button1         //         this.button1.Location = new System.Drawing.Point(24, 12);         this.button1.Name = "button1";         this.button1.Size = new System.Drawing.Size(75, 23);         this.button1.TabIndex = 0;         this.button1.Text = "抓取桌面";         this.button1.UseVisualStyleBackColor = true;         this.button1.Click += new System.EventHandler(this.button1_Click);         //         // pictureBox1         //         this.pictureBox1.Location = new System.Drawing.Point(105, 12);         this.pictureBox1.Name = "pictureBox1";         this.pictureBox1.Size = new System.Drawing.Size(515, 306);         this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;         this.pictureBox1.TabIndex = 1;         this.pictureBox1.TabStop = false;         //         // Form1         //         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(648, 330);         this.Controls.Add(this.pictureBox1);         this.Controls.Add(this.button1);         this.Name = "Form1";         this.Text = "Form1";         ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();         this.ResumeLayout(false);       }       #endregion       private System.Windows.Forms.Button button1;     private System.Windows.Forms.PictureBox pictureBox1; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public partial class Form1 : Form   {       public Form1()       {           InitializeComponent();       }           private void Form1_Load(object sender, EventArgs e)       {         }         private void button1_Click(object sender, EventArgs e)       {           Image memory = new Bitmap(300,200);           Graphics g = Graphics.FromImage(memory);           g.CopyFromScreen(0,0, 0, 0, new Size(300,200));           pictureBox1.Image = memory;       }   }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 partial class Form1 {     /// <summary>     /// 必需的设计器变量。     /// </summary>     private System.ComponentModel.IContainer components = null;       /// <summary>     /// 清理所有正在使用的资源。     /// </summary>     /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>     protected override void Dispose(bool disposing)     {         if (disposing && (components != null))         {             components.Dispose();         }         base.Dispose(disposing);     }       #region Windows 窗体设计器生成的代码       /// <summary>     /// 设计器支持所需的方法 - 不要     /// 使用代码编辑器修改此方法的内容。     /// </summary>     private void InitializeComponent()     {         this.pictureBox1 = new System.Windows.Forms.PictureBox();         this.button1 = new System.Windows.Forms.Button();         ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();         this.SuspendLayout();         //         // pictureBox1         //         this.pictureBox1.Location = new System.Drawing.Point(12, 12);         this.pictureBox1.Name = "pictureBox1";         this.pictureBox1.Size = new System.Drawing.Size(309, 200);         this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;         this.pictureBox1.TabIndex = 0;         this.pictureBox1.TabStop = false;         //         // button1         //         this.button1.Location = new System.Drawing.Point(327, 32);         this.button1.Name = "button1";         this.button1.Size = new System.Drawing.Size(131, 23);         this.button1.TabIndex = 1;         this.button1.Text = "抓取左上角的图片";         this.button1.UseVisualStyleBackColor = true;         this.button1.Click += new System.EventHandler(this.button1_Click);         //         // Form1         //         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(470, 225);         this.Controls.Add(this.button1);         this.Controls.Add(this.pictureBox1);         this.Name = "Form1";         this.Text = "Form1";         this.Load += new System.EventHandler(this.Form1_Load);         ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();         this.ResumeLayout(false);       }       #endregion       private System.Windows.Forms.PictureBox pictureBox1;     private System.Windows.Forms.Button button1; }

以上就是基于C#实现屏幕桌面截图的详细内容,更多关于C#屏幕桌面截图的资料请关注脚本之家其它相关文章!

标签:截图,桌面,C#,System,Windows,button1,Form1,pictureBox1,new
From: https://www.cnblogs.com/bruce1992/p/17015844.html

相关文章

  • C#数字图像处理图像旋转图片加角度
    原文网址:https://www.cnblogs.com/-jingzhe/p/14271797.htmlc#数字图像处理图像旋转 如果平面上的点绕原点逆时针旋转θº,则其坐标变换公式为:          ......
  • 【JetsonNano】移远EC200获取GPS定位及打电话
    硬件:JetsonNano、移远EC200模块操作系统:ubuntu18.041.安装驱动JstsonNano本身是没有USB转串口驱动的,所以我们需要先选择一个适合自己系统版本的驱动。1.1先通过unam......
  • AirBuddy for Mac(AirPods耳机管理工具)v2.6.3汉化版
    AirBuddy激活版哪里可以下载呢?AirBuddyforMac(AirPods耳机管理工具)v2.6.3汉化版分享给大家,Airbuddyformac是一款功能高效的AirPods耳机管理工具,可以帮助你体验AirPods......
  • MAC之PyQt5环境
    一,安装模块1,pyqt5pip3installpyqt52,pyqt5-toolspip3installpyqt5-tools二,pyqt5designer的位置安装了pyqt5后就有了:/Users/hz/WorkSpace/Python/Qt/venv/lib/python3.......
  • 软件icons图标大全(新增至2719枚大苏尔风格图标)下载
    想要获取海量图标,macw小编为大家分享一套mac电脑icons图标包,此套图标大全中包含两千多个.icns格式图片,这是一套不可多得的设计素材包。软件icons图标大全(新增至2719枚大......
  • 【归并排序】【链表】LeetCode 148. 排序链表
    题目链接148.排序链表思想分割cut环节:找到当前链表中点,并从中点将链表断开(以便在下次递归cut时,链表片段拥有正确边界)我们使用fast,slow快慢双指针法,奇数个......
  • #yyds干货盘点#linux cat命令详解
    cat命令:1、catlinux.txt:查看linux.txt内容。2、cat-nlinux.txt,查看linux.txt文件的内容,并且由1开始对所有输出行进行编号。(包括空白行)3、cat-blinux.txt,用法和-n......
  • Oracle_3_复制表、插入选择、递归查询
    一、复制表1、selectinto,使用查询结果新建表结构:createtable表名(字段名1,字段名2,......)asselect语句2、insertintoselect,使用查询结果插入到表中结构:inse......
  • 使用CSS提高网站性能的30种方法
    根据httparchive.org的页面重量报告,CSS在平均70个请求和2MB的网页上占7个HTTP请求和70Kb的代码。这并不是网站性能糟糕的最坏原因(我正看着你呢,JavaScript),但CSS面临着特定的......
  • git hook- prepare-commit-msg
    1.不能提交到masterproductionmain这几个分支2.分支名只能以feature|hotfix|bugfix|release|dev|improvement这几个开头3.自动加分支名到提交的消息上4.变基的情况......