现在的位置: 首页 > 综合 > 正文

c#裁剪图片(根据鼠标画的矩形裁剪图片)

2013年05月27日 ⁄ 综合 ⁄ 共 2638字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using S10806Class;
using System.IO;

namespace GetImage
{
public partial class GetScreenImgForm : Form
{

/// <summary>
/// 图像操作类
/// </summary>
ImageWork imageWork = new ImageWork();

/// <summary>
/// 保存截取到的桌面图像
/// </summary>
Image screenImg = null;

/// <summary>
/// 保存用户截取的图像区域
/// </summary>
Rectangle imgRect = new Rectangle();

/// <summary>
/// 窗体的GDI
/// </summary>
Graphics g = null;

/// <summary>
/// 区域边框画笔
/// </summary>
Pen pen = new Pen(Color.Black, 1);

/// <summary>
/// 是否应该绘制区域
/// </summary>
bool isDraw = false;

/// <summary>
/// 用户是否选择了区域
/// </summary>
bool isOption = false;

public GetScreenImgForm()
{
InitializeComponent();

//获取当前桌面截图
screenImg = imageWork.GetScreenImage();

g = this.CreateGraphics();
}

private void GetScreenImgForm_Load(object sender, EventArgs e)
{
this.Size = new Size(screenImg.Width, screenImg.Height);

this.BackgroundImage = screenImg;
}

private void GetScreenImgForm_MouseUp(object sender, MouseEventArgs e)
{
//没有截图并且单击右键 - 关闭本窗体
if (e.Button == MouseButtons.Right && !this.isOption)
{
this.Close();
}
//截图了并且单击右键 - 取消此次截图
else if (e.Button == MouseButtons.Right && this.isOption)
{
g.DrawImage(screenImg, new Point(0, 0));
this.isOption = false;
return;
}
//在截图情况下松开左键,选择区域结束,等待双击截取
else if (e.Button == MouseButtons.Left && this.isOption && this.isDraw)
{
this.isDraw = false;

imgRect.Width = e.X - imgRect.X; //区域长度

imgRect.Height = e.Y - imgRect.Y; //区域高度
}

}

private void GetScreenImgForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;

if (this.isOption) return;

imgRect.X = e.X; //起始X轴

imgRect.Y = e.Y; //起始Y轴

this.isDraw = true;//允许绘图
}

private void GetScreenImgForm_MouseMove(object sender, MouseEventArgs e)
{
if (isDraw)
{
//刷新图像
g.DrawImage(screenImg, new Point(0, 0));

this.isOption = true;

Point p1 = new Point(imgRect.X, imgRect.Y);
Point p2 = new Point(e.X, imgRect.Y);
Point p3 = new Point(imgRect.X, e.Y);
Point p4 = new Point(e.X, e.Y);

//画四条边框
g.DrawLine(pen, p1, p2);
g.DrawLine(pen, p2, p4);
g.DrawLine(pen, p4, p3);
g.DrawLine(pen, p3, p1);
}
else
{
//确定用户的选择区域
int top = imgRect.Y;
int down = imgRect.Y + imgRect.Height;
int left = imgRect.X;
int right = imgRect.X + imgRect.Width;

//鼠标在选择区域中会变个模样
if (e.X > left && e.X < right && e.Y > top && e.Y < down)
{
this.Cursor = Cursors.SizeAll;
}
else
{
this.Cursor = Cursors.Cross;
}
}
}

private void GetScreenImgForm_MouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
//用户双击选择区域时.....
if (this.Cursor == Cursors.SizeAll)
{
Bitmap b = new Bitmap(this.screenImg);

Bitmap img = b.Clone(this.imgRect, System.Drawing.Imaging.PixelFormat.DontCare);

SaveFileDialog dlg = new SaveFileDialog();

dlg.Filter = "(*.jpg)|*.jpg";

if (dlg.ShowDialog() != DialogResult.OK) return;

img.Save(dlg.FileName);

this.Close();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}

}
}<收起

10

抱歉!评论已关闭.