科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件运用C#制作屏幕捕捉程序(4)

运用C#制作屏幕捕捉程序(4)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

运用C#制作屏幕捕捉程序

来源:soft6 2008年5月15日

关键字: 捕捉 屏幕 C# Windows

  • 评论
  • 分享微博
  • 分享邮件

运用C#制作屏幕捕捉程序(4)

完整代码:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Imaging;


namespace FormCapture

{

///

/// Summary description for Form1.

///

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.PictureBox pictureBox1;

private System.Windows.Forms.Button button1;

///

/// Required designer variable.

///

private System.ComponentModel.Container components = null;

 

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();


//

// TODO: Add any constructor code after InitializeComponent call

//

}


///

/// Clean up any resources being used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

 

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

this.button1 = new System.Windows.Forms.Button();

this.pictureBox1 = new System.Windows.Forms.PictureBox();

this.SuspendLayout();

//

// button1

//

this.button1.BackColor = System.Drawing.SystemColors.ActiveBorder;

this.button1.ForeColor = System.Drawing.SystemColors.ControlDarkDark;

this.button1.Location = new System.Drawing.Point(272, 19);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(72, 27);

this.button1.TabIndex = 4;

this.button1.Text = "屏幕捕捉";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// pictureBox1

//

this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));

this.pictureBox1.Location = new System.Drawing.Point(16, 16);

this.pictureBox1.Name = "pictureBox1";

this.pictureBox1.Size = new System.Drawing.Size(240, 224);

this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

this.pictureBox1.TabIndex = 0;

this.pictureBox1.TabStop = false;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(358, 255);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1,

this.pictureBox1});

this.KeyPreview = true;

this.Name = "Form1";

this.Text = "屏幕捕捉程序";

this.ResumeLayout(false);

 

}

#endregion

 

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

private static extern bool BitBlt(

IntPtr hdcDest, file://目的DC的句柄

int nXDest, file://目的图形的左上角的x坐标

int nYDest, file://目的图形的左上角的y坐标

int nWidth, file://目的图形的矩形宽度

int nHeight, file://目的图形的矩形高度

IntPtr hdcSrc, file://源DC的句柄

int nXSrc, file://源图形的左上角的x坐标

int nYSrc, file://源图形的左上角的x坐标

System.Int32 dwRop file://光栅操作代码

);

 

private void button1_Click(object sender, System.EventArgs e)

{

Graphics g1 = this.CreateGraphics();//获得窗体图形对象

Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);

Graphics g2 = Graphics.FromImage(MyImage);//创建位图图形对象

IntPtr dc1 = g1.GetHdc();//获得窗体的上下文设备

IntPtr dc2 = g2.GetHdc();//获得位图文件的上下文设备

BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);//写入到位图

g1.ReleaseHdc(dc1);//释放窗体的上下文设备

g2.ReleaseHdc(dc2);//释放位图文件的上下文设备

MyImage.Save(@"c:\Captured.jpg", ImageFormat.Jpeg);//保存为jpeg文件

MessageBox.Show("保存图片结束!");

}

 

///

/// The main entry point for the application.

///

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

}

}

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章