1.调用截图窗体的代码:
Bitmap CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//新建一个和屏幕大小相同的图片
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(CatchBmp);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));//保存全屏图片
snapscreen screen = new snapscreen();
screen.BackgroundImage = CatchBmp;
screen.ShowDialog();
panel1.BackgroundImage = Clipboard.GetImage();
globalBitMap = panel1.BackgroundImage.Clone() as Bitmap;//保存副本
2.截屏弹出窗体的designer文件代码(vs生成的):
namespace Graphics
{
partial class snapscreen
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// snapscreen
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(394, 372);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Location = new System.Drawing.Point(1000, 600);
this.Name = "snapscreen";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "snapscreen";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.snapscreen_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.snapscreen_KeyDown);
this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.snapscreen_MouseDoubleClick);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.snapscreen_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.snapscreen_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.snapscreen_MouseUp);
this.ResumeLayout(false);
}
#endregion
}
}
3.cs文件的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Graphics
{
public partial class snapscreen : Form
{
System.Drawing.Graphics global_graphics;
Pen pen = new Pen(Brushes.Black);
private Point DownPoint;
private bool isEditting = false;
private Bitmap originBmp;
private Rectangle catchRect;
public snapscreen()
{
InitializeComponent();
global_graphics = this.CreateGraphics();
}
private void snapscreen_Load(object sender, EventArgs e)
{
originBmp = new Bitmap(this.BackgroundImage);
}
private void snapscreen_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
private void snapscreen_MouseDown(object sender, MouseEventArgs e)
{
DownPoint = e.Location;
isEditting = true;
}
private void snapscreen_MouseUp(object sender, MouseEventArgs e)
{
isEditting = false;
}
private void snapscreen_MouseMove(object sender, MouseEventArgs e)
{
if(isEditting == true)
{
Point nowPoint = e.Location;
int x1 = Math.Min(DownPoint.X, nowPoint.X);
int y1 = Math.Min(DownPoint.Y, nowPoint.Y);
int width = Math.Max(DownPoint.X, nowPoint.X) - Math.Min(DownPoint.X, nowPoint.X);
int height = Math.Max(DownPoint.Y, nowPoint.Y) - Math.Min(DownPoint.Y, nowPoint.Y);
//基于背景图创建一个画布,并绘制矩形,然后在窗体上显示
Bitmap destBmp = (Bitmap)originBmp.Clone();
//在新建图片上创建画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(destBmp);
catchRect = new Rectangle(new Point(x1, y1), new Size(width, height));
Pen p1 = new Pen(Color.Black, 1);
g.DrawRectangle(p1, catchRect); //将矩形绘制到画板上
g.Dispose(); //释放目前的画板
p1.Dispose();
global_graphics.DrawImage(destBmp, new Point(0, 0));
destBmp.Dispose();
}
}
private void snapscreen_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (catchRect.Contains(new Point(e.X, e.Y)))
{
Image CatchedBmp = new Bitmap(catchRect.Width, catchRect.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(CatchedBmp); //创建图片画布
//源范围
Rectangle sourceRectangle = new Rectangle(0, 0, catchRect.Width, catchRect.Height);
//目标范围 catchRect
g.DrawImage(originBmp, sourceRectangle, catchRect, GraphicsUnit.Pixel);
Clipboard.SetImage(CatchedBmp);//将图片保存到剪贴板
g.Dispose();
CatchedBmp.Dispose();
originBmp.Dispose();
this.Close();
this.Dispose();
}
}
}
}
- 主要思路:
(1) 全屏的截屏文件保存在内存中,并基于该图片创建一个画布 graphics1
(2) 在mousemove事件中,向graphics1 画布上添加矩形边线(对应着截图区)
(3) 在mousedoubleclick事件中,若点击坐标在矩形选区内,则视作保存选区。
(4)创建一个与选区相同大小的image,将选区复制到该图片中。
(5)调用剪贴板函数 Clipboard.SetImage(CatchedBmp);//将图片保存到剪贴板