惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
博客园 - 叶小钗
雷峰网
雷峰网
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
量子位

博客园 - 点点小铺

招聘Delphi程序员,懂.Net----广州地区(08年4月1日前有效) 用C# 实现C/S模式下软件自动在线升级 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)--来源CSDN 使用ADOX创建Access数据库和表-- 来源于CSDN 运用C#制作屏幕捕捉程序(1) 通过WMI获得硬盘和CPU的物理序列号 探讨C#中字符串的加密 在 C# 中加载自己编写的动态链接库 用C#生成随机中文汉字验证码的基本原理 C#中如何读写INI文件 c#中结构与类的区别 C#中对注册表的操作 C#中调用Windows API的要点 C#的多线程机制初探(4) C#的多线程机制初探(3) C#的多线程机制初探(2) C#的多线程机制初探(1) C#中如何插入照片到Excel C#编程让Outlook乖乖交出帐户密码
运用C#制作屏幕捕捉程序(2)
点点小铺 · 2006-10-09 · via 博客园 - 点点小铺

这样,基本的原理就已介绍完毕,总的来说实现的原理还是比较简单的。下面就是实现实例的具体步骤:

1

获得一个对应于屏幕上窗体的图形对象

2

创建一个大小为窗体客户区大小的位图文件

3

获得窗体的上下文设备

4

获得位图文件的上下文设备

5

把屏幕上的窗体写入位图中

6

释放窗体的上下文设备

7

释放位图文件的上下文设备

8

将图像保存为一个jpeg格式的文件 工程步骤:

1

新建一个C#的工程,不妨命名为“FormCapture”,图示如下:

2

布置主窗体:   在窗体上添加一个图片框(pictureBox)控件以及一个按钮(button)控件即可。将主窗体的Text属性设置为屏幕捕捉程序;将图片框的Image属性设置为一个图形文件;将按钮的Text属性设置为屏幕捕捉即可。(当然读者可以添加自己需要的控件来布置主窗体)图示如下:

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

3

代码编写: 先在代码文件中添加上面介绍的BitBlt函数的外部声明。 再添加按钮的OnClick事件处理函数,该函数就是实现屏幕捕捉的主要部分了。函数如下: 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("

保存图片结束!");

}

4

.到此为止,程序已经做完了。按CtrlF5可以试试效果如下图: 图片已经保存好了,看看屏幕捕捉的结果吧(如下图)!   不过,这个程序只是捕捉到了程序自身的客户区,所以功能有限。当然,你也可以试着做个捕捉屏幕任何位置的程序!你只要改变源图像的宽度和高度即可,而这个宽度和高度当然可以由用户来选定。这样,一个自制的屏幕捕捉程序就出炉了。   从上面的实例中,我们不难发现用C#编程实现一些基本功能还是非常容易的,真可谓是高效开发的好工具。所以,笔者希望有更多的人加入C#的行列,开发出更实用、更完善的软件。

运用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, //

目的DC的句柄

int nXDest, //

目的图形的左上角的x坐标

int nYDest, //

目的图形的左上角的y坐标

int nWidth, //

目的图形的矩形宽度

int nHeight, //

目的图形的矩形高度

IntPtr hdcSrc, //

DC的句柄

int nXSrc, //

源图形的左上角的x坐标

int nYSrc, //

源图形的左上角的x坐标

System.Int32 dwRop //

光栅操作代码

);

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());

}

}

}