您的位置:首页 > 其它

学习笔记:GDI,截图功能实现

2012-11-04 18:22 405 查看
(以下是参考了作者:stg609的博客,自己动手做后,写下来的步骤,方便自己以后复习,无剽窃他人作品之意,勿喷!)
需求:基本实现截图功能.
工具:Visual studio 2008 (winform).
结构:共有两个form,第一个form为:originForm
由一个button控件和一个richTextBox组成.

第一个form的代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Threading;

namespace Catch

{

public partialclass originForm : Form

{

publicoriginForm()

{

InitializeComponent();

}

privatevoid button1_Click(object sender, EventArgs e)

{

this.Hide();//隐藏窗体

Thread.Sleep(50);//线程休息

CatchCatchForm = new Catch();//新建一个名为CatchForm的窗体

BitmapCatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width,Screen.AllScreens[0].Bounds.Height);//创建一个宽高与屏幕大小相同的位图

Graphics g = Graphics.FromImage(CatchBmp);

g.CopyFromScreen(new Point(0, 0), new Point(0, 0), newSize(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));
//将屏幕图像复制到位图中

CatchForm.BackgroundImage = CatchBmp;

if(CatchForm.ShowDialog() == DialogResult.OK)

{

IDataObject iData = Clipboard.GetDataObject();

DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);

if(iData.GetDataPresent(DataFormats.Bitmap))

{

richTextBox1.Paste(myFormat);

Clipboard.Clear();

}

this.Show();

}

}

}

}


第二个form:Catch.在其属性中,将FormBorderStyle设为None.WindowState设为Maximized.

Catch中的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Catch
{
public partial class Catch : Form
{
private Point DownPoint = Point.Empty;//鼠标左键按下的坐标
private bool CatchFinished = false;//确定截图完成
private bool CatchStart = false;//表示截图开始
private Bitmap originBmp;//保存原始图像
private Rectangle CatchRect;//保存截图的矩形
public Catch()
{
InitializeComponent();
}

private void Catch_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);//双缓冲,可减少图片闪烁
this.UpdateStyles();
originBmp = new Bitmap(this.BackgroundImage);//将整个屏幕的图片放到originBmp中
}

private void Catch_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)//鼠标点击右键,完成截图
{
this.DialogResult = DialogResult.OK;//点击右键后,将DialogResult.OK赋值给DialogResult
this.Close();//关闭窗口
}
}

private void Catch_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//点击鼠标左键
{
if (CatchStart == false)
{
CatchStart = true;//开始截图
DownPoint = new Point(e.X, e.Y);//记录点击点坐标
}
}
}

private void Catch_MouseMove(object sender, MouseEventArgs e)
{
if (CatchStart)//如果开始截图
{
Bitmap destBmp = (Bitmap)originBmp.Clone();
Point newPoint = new Point(DownPoint.X, DownPoint.Y);
Graphics g = Graphics.FromImage(destBmp);
Pen p = new Pen(Color.Blue, 1);
int width = Math.Abs(e.X - DownPoint.X);//X坐标的差的绝对值为宽
int height = Math.Abs(e.Y - DownPoint.Y);//Y坐标的差的绝对值为高
if (e.X < DownPoint.X)
{
newPoint.X = e.X;
}
if (e.Y < DownPoint.Y)
{
newPoint.Y = e.Y;
}
CatchRect = new Rectangle(newPoint, new Size(width, height));//获得新的矩形
g.DrawRectangle(p, CatchRect);
g.Dispose();//释放画板
p.Dispose();
Graphics g1 = this.CreateGraphics();
g1 = this.CreateGraphics();
g1.DrawImage(destBmp, new Point(0, 0));//将刚才所画的图片画到这个窗体上
g1.Dispose();
destBmp.Dispose();
}
}

private void Catch_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (CatchStart)
{
CatchStart = false;
CatchFinished = true;//标记为截图完成状态
}
}
}

private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && CatchFinished)//在截图的矩形中,双击鼠标,完成截图.此处判断有无左键双击,并且截图状态是否属于完成状态.
{
if(CatchRect.Contains(new Point(e.X,e.Y)))//判断有无在矩形中
{
Bitmap CatchedBmp = new Bitmap(CatchRect.Width,CatchRect.Height);//新建CatchedBmp
Graphics g = Graphics.FromImage(CatchedBmp);//在CatchedBmp上创建画板
g.DrawImage(originBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel);//将截图画上CatchedBmp
Clipboard.SetImage(CatchedBmp);//复制到粘贴板中
g.Dispose();
CatchFinished = false;
CatchedBmp.Dispose();
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}

}
}


参考资料:http://www.cnblogs.com/stg609/archive/2008/03/19/1113694.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: