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

 更新时间:2022年12月12日 14:50:31   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#实现屏幕桌面截图以及左上角区域截图功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以了解一下

实践过程

效果

代码

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;
        }
    }
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;
}
  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;
        }
    }
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#语音识别用法实例

    C#语音识别用法实例

    这篇文章主要介绍了C#语音识别用法,实例分析了C#利用微软操作系统自动的语音识别功能,读取信息的技巧,需要的朋友可以参考下
    2015-01-01
  • c# 实现IComparable、IComparer接口、Comparer类的详解

    c# 实现IComparable、IComparer接口、Comparer类的详解

    本篇文章是对c#中实现IComparable、IComparer接口、Comparer类进行了详细的分析详解,需要的朋友参考下
    2013-05-05
  • C#列出所有物理网络适配器的方法

    C#列出所有物理网络适配器的方法

    这篇文章主要介绍了C#列出所有物理网络适配器的方法,实例分析了C#操作网络设备的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • Asp.Net中MVC缓存详解

    Asp.Net中MVC缓存详解

    这篇文章主要介绍了Asp.Net中MVC缓存的种类区别等内容,一下来学习下。
    2017-12-12
  • RSA密钥--JAVA和C#的区别及联系

    RSA密钥--JAVA和C#的区别及联系

    这篇文章主要介绍了关于RSA密钥事件JAVA和C#的区别及联系,文章从RSA语法介绍开始展开详细介绍了C#转JAVA及JAVA转C#,需要的小伙伴可以可以参考一下
    2021-10-10
  • C#通过经纬度计算2个点之间距离的实现代码

    C#通过经纬度计算2个点之间距离的实现代码

    这篇文章主要介绍了C#通过经纬度计算2个点之间距离实现代码,本文对实现原理、经纬度基本知识等一并做了讲解,需要的朋友可以参考下
    2014-08-08
  • C#自定义控件添加右键菜单的方法

    C#自定义控件添加右键菜单的方法

    这篇文章主要介绍了C#自定义控件添加右键菜单的方法,本文用到control控件,专门自定义右键菜单,下面小编给大家整理下,有需要的小伙伴可以来参考下
    2015-08-08
  • 如何让C#、VB.NET实现复杂的二进制操作

    如何让C#、VB.NET实现复杂的二进制操作

    VB.NET和C#属于高级语言,对二进制位操作的支持不是很好,比如没有了移位运算等,用的时候确实很不方便,所以在闲暇之余我重新封装了一个用于C#、VB.NET的位操作类库,通过该类库可以实现数据移位、循环移位、转换为二进制、将二进制转换为数据等
    2013-07-07
  • C# 控件属性和InitializeComponent()关系案例详解

    C# 控件属性和InitializeComponent()关系案例详解

    这篇文章主要介绍了C# 控件属性和InitializeComponent()关系案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C#测量程序运行时间及cpu使用时间实例方法

    C#测量程序运行时间及cpu使用时间实例方法

    对一个服务器程序想统计每秒可以处理多少数据包,要如何做?答案是用处理数据包的总数,除以累记处理数据包用的时间,下面我们看一个代码实例就明白了
    2013-11-11

最新评论