65.9K
CodeProject 正在变化。 阅读更多。
Home

创建你自己的截图工具

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.66/5 (19投票s)

2012年10月31日

CPOL

2分钟阅读

viewsIcon

101539

这个工具可以捕捉您桌面上任何东西的屏幕截图,例如图片或网页的一部分。 这个工具允许您使用快捷键选择整个屏幕的部分区域。

引言

在本文中,我将向您展示如何创建您自己的截图工具。 这个工具可以捕捉您桌面上任何东西的屏幕截图,例如图片或网页的一部分。 这个工具允许您选择整个屏幕的部分区域。

背景

在 Windows 7 中的截图工具出现之前,编辑快照的常用方法是按 Print Screen 键,打开画图工具(或任何其他图像编辑器),裁剪区域并复制它。 有了这个工具,就不再需要图像编辑器了。 这个小项目也可以用作其他项目(如 OCR 或图像处理器)的基础。

使用代码

首先,我们必须设置表单属性。 这可以通过手动或代码来完成

  • BackColor - 设置为您希望在图像本身未覆盖的区域中显示的任何颜色。 我更喜欢黑色。
  • FormBorderStyle - 无
  • StartPosition - 手动
  • 顶部 - 0
  • 左侧 - 0
  • WindowState - Maximized(奇怪的是这是必需的。 如果您不设置它,表单将不会完全全屏显示)。

向表单添加一个 PictureBox 控件。 设置这些属性

  • Location - 0, 0
  • SizeMode - 缩放(确保图像以正确的纵横比显示)

将此代码设置到表单加载事件中

//Hide the Form
this.Hide();
//Create the Bitmap
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                         Screen.PrimaryScreen.Bounds.Height);
//Create the Graphic Variable with screen Dimensions
Graphics graphics = Graphics.FromImage(printscreen as Image);
//Copy Image from the screen
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
//Create a temporal memory stream for the image
using (MemoryStream s = new MemoryStream())
{
    //save graphic variable into memory
    printscreen.Save(s, ImageFormat.Bmp);                
    pictureBox1.Size = new System.Drawing.Size(this.Width, this.Height);
    //set the picture box with temporary stream
    pictureBox1.Image = Image.FromStream(s);
}
//Show Form
this.Show();
//Cross Cursor
Cursor = Cursors.Cross;

现在您必须声明一些私有变量,用于鼠标位置和绘制裁剪区域

//These variables control the mouse position
int selectX;
int selectY;
int selectWidth;
int selectHeight;
public Pen selectPen;        

//This variable control when you start the right click
bool start = false;

鼠标按下和鼠标移动事件将用于创建此效果。 这是鼠标移动事件的代码

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    //validate if there is an image
    if (pictureBox1.Image == null)
        return;            
    //validate if right-click was trigger
    if(start)
    {
        //refresh picture box
        pictureBox1.Refresh();
        //set corner square to mouse coordinates
        selectWidth = e.X - selectX;
        selectHeight = e.Y - selectY;
        //draw dotted rectangle
        pictureBox1.CreateGraphics().DrawRectangle(selectPen, 
                  selectX, selectY, selectWidth, selectHeight);
    }
}

这是鼠标按下事件的代码

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    //validate when user right-click
    if (!start)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            //starts coordinates for rectangle
            selectX = e.X;
            selectY = e.Y;
            selectPen = new Pen(Color.Red, 1);
            selectPen.DashStyle = DashStyle.DashDotDot;
        }
        //refresh picture box
        pictureBox1.Refresh();
        //start control variable for draw rectangle
        start = true;
    }
    else
    {
        //validate if there is image
        if (pictureBox1.Image == null)
            return;
        //same functionality when mouse is over
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            pictureBox1.Refresh();
            selectWidth = e.X - selectX;
            selectHeight = e.Y - selectY;
            pictureBox1.CreateGraphics().DrawRectangle(selectPen, selectX, 
                     selectY, selectWidth, selectHeight);

        }
        start = false;
        //function save image to clipboard
        SaveToClipboard();            
    }
}

鼠标移动事件有变量 selectXselectY。 它们将在鼠标按下事件将布尔变量 start 设置为 true 或 false 后保存初始鼠标位置,这意味着开始裁剪操作或分别结束。借助事件参数,我们可以轻松确定矩形的宽度和高度。

private void SaveToClipboard()
{
    //validate if something selected
    if (selectWidth > 0)
    {

        Rectangle rect = new Rectangle(selectX, selectY, selectWidth, selectHeight);
        //create bitmap with original dimensions
        Bitmap OriginalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
        //create bitmap with selected dimensions
        Bitmap _img = new Bitmap(selectWidth, selectHeight);
        //create graphic variable
        Graphics g = Graphics.FromImage(_img);
        //set graphic attributes
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);                
        //insert image stream into clipboard
        Clipboard.SetImage(_img);
    } 
    //End application
    Application.Exit();
}

SaveToClipboard 函数将对象 picture box 在其图像缓冲区中的内容保存到剪贴板内存中,并设置所有属性(如大小和质量)。

因此,每次您执行该应用程序时,您都能够冻结屏幕图像并裁剪您希望的屏幕片段,并自动将其发送到剪贴板,准备好使用快捷键 ctrl + V 进行粘贴。

每次需要都去结束并执行系统可能不实用,因此您可以通过 Windows 快捷方式创建一个快捷键。 转到应用程序所在的位置,然后在 .exe 文件上单击鼠标右键,指向新建快捷方式。

然后右键单击快捷方式,选择属性,然后选择快捷键,然后按您希望调用该功能的键(例如 [Ctrl + Alt + C]),就这样,每次您按 [Ctrl + Alt + C] 时,您将冻结屏幕,并且您将能够在屏幕中裁剪图像。

© . All rights reserved.