先给你的程序添加一个Windows窗体 ,Name:ScreenBody TopMost:true WindowState:Maximized
下面是一些字段定义,事件函数和辅助函数:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LCY.XY
{
public partial class ScreenBody : Form
{
/// <summary>
/// 主画笔
/// </summary>
private Graphics mainPainter;
/// <summary>
/// 笔
/// </summary>
private Pen pen;
/// <summary>
/// 判断鼠标是否按下
/// </summary>
private bool isDowned;
/// <summary>
/// 矩形是否绘制完成
/// </summary>
private bool rectReady;
/// <summary>
/// 原始画面
/// </summary>
private Image baseImage;
/// <summary>
/// 要保存的画面
/// </summary>
private Rectangle rect;
/// <summary>
/// 鼠标按下的点
/// </summary>
private Point downPoint;
private int tmpx;
private int tmpy;
public ScreenBody()
{
InitializeComponent();
}
/// <summary>
/// 截图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_DoubleClick(object sender, EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Left && rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
{
Image memory = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(memory);
g.CopyFromScreen(rect.X + 1, rect.Y + 1, 0, 0, rect.Size);
Clipboard.SetImage(memory);
this.Close();
}
}
/// <summary>
/// 按下鼠标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDowned = true;
if (!rectReady)
{
rect.X = e.X;
rect.Y = e.Y;
downPoint = new Point(e.X, e.Y);
}
if (rectReady)
{
tmpx = e.X;
tmpy = e.Y;
}
}
if (e.Button == MouseButtons.Right)
{
if (!rectReady)
{
this.Close();
return;
}
mainPainter.DrawImage(baseImage, 0, 0);
rectReady = false;
}
}
/// <summary>
/// 松开鼠标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X < rect.X)
{
rect.X = e.X;
}
if (e.Y < rect.Y)
{
rect.Y = e.Y;
}
isDowned = false;
rectReady = true;
}
}
/// <summary>
/// 移动鼠标
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
{
if (!rectReady && isDowned)
{
Image newImage = drawScreen((Image)baseImage.Clone(), e.X, e.Y);
mainPainter.DrawImage(newImage, 0, 0);
newImage.Dispose();
}
if (rectReady)
{
if (rect.Contains(e.X, e.Y))
{
if (isDowned)
{
rect.X = rect.X + e.X - tmpx;
rect.Y = rect.Y + e.Y - tmpy;
tmpx = e.X;
tmpy = e.Y;
moveRect((Image)baseImage.Clone(), rect);
}
}
}
}
/// <summary>
/// 画矩形
/// </summary>
/// <param name="Painter"></param>
/// <param name="mouse_x"></param>
/// <param name="mouse_y"></param>
private void drawRect(Graphics Painter, int mouse_x, int mouse_y)
{
int width = 0;
int heigth = 0;
if (mouse_y < rect.Y)
{
heigth = downPoint.Y - mouse_y;
}
else
{
heigth = mouse_y - downPoint.Y;
}
if (mouse_x < rect.X)
{
width = downPoint.X - mouse_x;
}
else
{
width = mouse_x - downPoint.X;
}
rect.Size = new Size(width, heigth);
Painter.DrawRectangle(pen, minOf(mouse_x, downPoint.X), minOf(mouse_y, downPoint.Y), width, heigth);
}
/// <summary>
/// 选区域时画矩形
/// </summary>
/// <param name="back"></param>
/// <param name="mouse_x"></param>
/// <param name="mouse_y"></param>
/// <returns></returns>
private Image drawScreen(Image back, int mouse_x, int mouse_y)
{
Graphics painter = Graphics.FromImage(back);
drawRect(painter, mouse_x, mouse_y);
return back;
}
/// <summary>
/// 选好区域后移动矩形
/// </summary>
/// <param name="image"></param>
/// <param name="rect2"></param>
private void moveRect(Image image, Rectangle rect2)
{
Graphics painter = Graphics.FromImage(image);
painter.DrawRectangle(pen, rect2.X, rect2.Y, rect2.Width, rect2.Height);
mainPainter.DrawImage(image, 0, 0);
image.Dispose();
}
/// <summary>
/// 初始化变量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenBody_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
mainPainter = this.CreateGraphics();
pen = new Pen(Brushes.Blue);
isDowned = false;
baseImage = this.BackgroundImage;
rect = new Rectangle();
rectReady = false;
}
/// <summary>
/// 返回较小的数
/// </summary>
/// <param name="f1"></param>
/// <param name="f2"></param>
/// <returns></returns>
private float minOf(float f1, float f2)
{
if (f1 > f2)
return f2;
else
return f1;
}
}
}
最后,在需要的地方调用以下函数:
/// <summary>
/// 显示截图窗口
/// </summary>
void GetBitmap()
{
Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
ScreenBody body = new ScreenBody();
body.BackgroundImage = img;
body.Show();
}