一、
C#屏幕截图
先给你的程序添加一个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);
ret
urn 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/
// <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();
}
二、C# 实现鼠标选择矩形截图
private void button1_Click(object sender, EventArgs e)
{
s.GerScreenFormRectangle(); 
}
private Zgke.CopyScreen s;
private void Form1_Load(object sender, EventArgs e)
{
s = new Zgke.CopyScreen();
s.GetScreenImage+=new Zgke.CopyScreen.GetImage(s_GetScreenImage);
}
void s_GetScreenImage(Image p_Image)
{
pictureBox1.Image = p_Image;
}
当按下BUTTON1的时候可以在屏幕上选择一个矩形进行截图
全部的类
先建立个新项目 为WINDOWS库类.这里因为需要全局钩子..我在WINFORM里不知道为什么设置不了全局钩子.
但在DLL里就可以.....可恶的C#........
复制下面代码到CLASS1.CS里
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.Drawing; 
using System.Windows.Forms
namespace Zgke 
public class API 
[DllImport("user32.dll", CharSet = CharSet.Auto)] 
public static extern IntPtr GetDesktopWindow(); 
[DllImport("user32.dll", CharSet = CharSet.Auto)] 
public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid); 
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); 
[DllImport("kernel32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] 
public static extern int GetCurrentThreadId(); 
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
public static extern bool UnhookWindowsHookEx(IntPtr hhook); 
public enum WindowsHookCodes 
WH_MSGFILTER = (-1), 
WH_JOURNALRECORD = 0, 
WH_JOURNALPLAYBACK = 1, 
WH_KEYBOARD = 2, 
WH_GETMESSAGE = 3, 
WH_CALLWNDPROC = 4, 
WH_CBT = 5, 
WH_SYSMSGFILTER = 6, 
WH_MOUSE = 7, 
WH_HARDWARE = 8, 
WH_DEBUG = 9, 
WH_SHELL = 10, 
WH_FOREGROUNDIDLE = 11, 
WH_CALLWNDPROCRET = 12, 
WH_KEYBOARD_LL = 13, 
WH_MOUSE_LL = 14 
/// <summary> 
/// 一个根据矩形截图类 
/// zgke@Sina 
/
// qq:116149 
/// </summary> 
public class CopyScreen 
/// <summary> 
/// 屏幕大小 
/// </summary> 
private Size ScreenSize { get { return Screen.PrimaryScreen.Bounds.Size; } } 
/// <summary> 
/// 鼠标位置 
/// </summary> 
private Point MousePoint { get { return Cursor.Position; } } 
/// <summary> 
/// 私有方法获取屏幕图形(全部图形) 
/// </summary> 
public Bitmap ScreenImage 
get 
{                 
Bitmap m_BackBitmap = new Bitmap(ScreenSize.Width, ScreenSize.Height); 
Graphics _Graphics = Graphics.FromImage(m_BackBitmap); 
_Graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), ScreenSize); 
_Graphics.Dispose(); 
return m_BackBitmap; 
/// <summary> 
/// 钩子 
/// </summary> 
private HookMessage m_HookMessage; 
/// <summary> 
/// 屏幕句柄 
/// </summary> 
private IntPtr m_ScreenForm; 
/// <summary> 
/// 图形 
/// </summary> 
private Bitmap m_Image; 
public delegate void GetImage(Image p_Image); 
/// <summary> 
/// 获取屏幕截图 
/// </summary> 
public event GetImage GetScreenImag
e; 
/
// <summary> 
/// 构造 
/// </summary> 
public CopyScreen() 
m_ScreenForm = API.GetDesktopWindow(); 
m_HookMessage = new HookMessage(API.WindowsHookCodes.WH_MOUSE_LL, true); 
m_HookMessage.GetHook += new HookMessage.GetHookMessage(m_HookMessage_GetHook); 
/// <summary> 
/// 钩子事件 
/
// </summary> 
/// <param name="p_Code"></param> 
/// <param name="p_wParam"></param> 
/// <param name="p_lParam"></param> 
/// <param name="p_Send"></param> 
void m_HookMessage_GetHook(int p_Code, IntPtr p_wParam, IntPtr p_lParam, ref bool p_Send) 
if (m_StarMouse) 
switch (p_wParam.ToInt32()) 
case 512: //Move 
MouseMove(); 
break; 
case 513:  //Down 
MouseDown(); 
p_Send = false; 
break; 
case 514:  //Up 
MouseUp(); 
p_Send = false; 
break; 
default: 
m_StarMouse = false; 
break; 
/// <summary> 
/// 根据矩形 如果Width是正直接返回 如果使-会转换成正的矩形 保证大小位置不变 
/// </summary> 
/// <param name="p_Rectangle">矩形</param> 
/// <returns>正矩形</returns> 
public static Rectangle GetUprightRectangle(Rectangle p_Rectangle) 
Rectangle _Rect = p_Rectangle; 
if (_Rect.Width < 0) 
int _X = _Rect.X; 
_Rect.X = _Rect.Width + _Rect.X; 
_Rect.Width = _X - _Rect.X; 
if (_Rect.Height < 0) 
int _Y = _Rect.Y; 
_Rect.Y = _Rect.Height + _Rect.Y; 
_Rect.Height = _Y - _Rect.Y; 
return _Rect; 
private Rectangle m_MouseRectangle = new Rectangle(0, 0, 0, 0); 
private bool m_DrawStar = false; 
private void MouseDown() 
m_MouseRectangle.X = MousePoint.X; 
m_MouseRectangle.Y = MousePoint.Y; 
m_DrawStar = true; 
private void MouseMove() 
if (m_DrawStar) 
ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.Transparent, FrameStyle.Dashed); 
m_MouseRectangle.Width = MousePoint.X - m_MouseRectangle.X; 
m_MouseRectangle.Height = MousePoint.Y - m_

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。