屏幕抓图一直使用一款只有 16K 大小的工具(
Capture),非常很方便,但在Windows2008下不兼容,无法抓图,因此自己写一个,运行环境为 .NET 2.0,使用方式完全与上述的Catpure相同,并且只有 8K 大小,截图质量更好。
使用截图如下:
代码如下:
C#:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Capture.Properties;
namespace Xianfen.Net.Capture
{
/// <summary>
///
/// 屏幕截图工具
///
/// http://www.xianfen.net
///
/// Ver:1.0
///
/// 2008-07-01
///
/// 更新:2010-09-07
/// 1. 鼠标光标嵌入到资源文件中,以防系统中相应的光标被删除后程序出错。
/// 2. 解决X, Y方向截图缺少一个像素的bug。
/// 3. 增加了多显示器的支持。
///
/// </summary>
public class Capture : Form
{
PictureBox pictureBox;
bool isDown = false;
Point downPos;
Point upPos;
Rectangle rect;
Bitmap bmp;
Graphics g;
public Capture()
{
pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.BorderStyle = BorderStyle.None;
pictureBox.MouseDown += new MouseEventHandler(pictureBox_MouseDown);
pictureBox.MouseMove += new MouseEventHandler(pictureBox_MouseMove);
pictureBox.MouseUp += new MouseEventHandler(pictureBox_MouseUp);
Stream stream = new MemoryStream(Resources.pen_rm);
this.Cursor = new Cursor(stream);
this.Controls.Add(pictureBox);
this.ShowInTaskbar = false;
this.DoubleBuffered = true;
this.Size = new Size(0, 0);
this.Load += new EventHandler(Capture_Load);
}
void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
isDown = true;
downPos = e.Location;
}
void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (isDown)
{
pictureBox.Refresh();
ControlPaint.DrawReversibleFrame(
new Rectangle(downPos.X, downPos.Y, e.X - downPos.X + 1, e.Y - downPos.Y + 1),
Color.Black, FrameStyle.Dashed);
}
}
void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDown = false;
upPos = e.Location;
if (upPos.X < downPos.X)
{
upPos.X = upPos.X + downPos.X;
downPos.X = upPos.X - downPos.X;
upPos.X = upPos.X - downPos.X;
}
if (upPos.Y < downPos.Y)
{
upPos.Y = upPos.Y + downPos.Y;
downPos.Y = upPos.Y - downPos.Y;
upPos.Y = upPos.Y - downPos.Y;
}
if (upPos.X - downPos.X > 0 && upPos.Y - downPos.Y > 0)
{
try
{
Bitmap targetBmp = new Bitmap(upPos.X - downPos.X + 1, upPos.Y - downPos.Y + 1);
Graphics targetG = Graphics.FromImage(targetBmp);
targetG.DrawImage(bmp, new Rectangle(0, 0, upPos.X - downPos.X + 1, upPos.Y - downPos.Y + 1),
downPos.X, downPos.Y, upPos.X - downPos.X + 1, upPos.Y - downPos.Y + 1, GraphicsUnit.Pixel);
targetG.Dispose();
bmp.Dispose();
g.Dispose();
Clipboard.SetImage(targetBmp);
}
catch
{ }
}
Close();
}
void Capture_Load(object sender, EventArgs e)
{
rect = Screen.FromControl(pictureBox).Bounds;
bmp = new Bitmap(rect.Width, rect.Height);
g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, rect.Size);
pictureBox.Image = bmp;
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.Run(new Capture());
MessageBox.Show("选择的图形已复制到剪切板\r\n在其它应用程序中可以用粘贴命令取出。",
"飘遥的Blog: http://www.xianfen.net",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{ }
}
}
}
源程序及可执行文件下载:
点击下载