飘遥的Blog

C/C++/.NET
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
    屏幕抓图一直使用一款只有 16K 大小的工具(Capture),非常很方便,但在Windows2008下不兼容,无法抓图,因此自己写一个,运行环境为 .NET 2.0,使用方式完全与上述的Catpure相同,并且只有 8K 大小,截图质量更好。
    使用截图如下:


代码如下:
C#:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Xianfen.Net.Capture
{
    
/// <summary>
    
///
    
/// 屏幕截图工具
    
///
    
/// http://www.xianfen.net
    
///
    
/// Ver:1.0
    
///
    
/// 2008-07-01
    
///
    
/// </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);

            
this.Cursor = new Cursor(Environment.SystemDirectory + @"\..\Cursors\pen_rm.cur");
            
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, e.Y - downPos.Y),
                    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, upPos.Y - downPos.Y);
                    Graphics targetG
= Graphics.FromImage(targetBmp);

                    targetG.DrawImage(bmp,
new Rectangle(0, 0, upPos.X - downPos.X, upPos.Y - downPos.Y),
                        downPos.X, downPos.Y, upPos.X
- downPos.X, upPos.Y - downPos.Y, GraphicsUnit.Pixel);

                    targetG.Dispose();
                    bmp.Dispose();
                    g.Dispose();

                    Clipboard.SetImage(targetBmp);
                }
                
catch
                {
                }
            }

            Close();
        }

        
void Capture_Load(object sender, EventArgs e)
        {
            rect
= Screen.PrimaryScreen.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;
        }
    }
}

可执行文件下载:点击下载