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

要详细点的获得鼠标画出的矩形和裁剪图片的算法会的大哥们帮帮忙啊!!!... 要详细点的 获得鼠标画出的矩形和裁剪图片的算法
会的大哥们帮帮忙啊!!!
展开
 我来答
百度网友217a1b5
推荐于2016-02-16 · TA获得超过360个赞
知道小有建树答主
回答量:1415
采纳率:0%
帮助的人:431万
展开全部
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);
}
}

}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式