这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:

 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;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using ImageClassLib;
namespace ImageShear
{
public partial class Demo: Form
{
public Demo()
{
InitializeComponent();
}
#region 点击打开图像
public string strHeadImagePath; //打开图片的路径
Bitmap Bi; //定义位图对像
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp"; //设置读取图片类型
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
strHeadImagePath = openFileDialog1.FileName;
//this.Show(strHeadImagePath);
Bi = new Bitmap(strHeadImagePath); //使用打开的图片路径创建位图对像
ImageCut1 IC = new ImageCut1(, , this.pictureBox1.Width, this.pictureBox1.Height); //实例化ImageCut类,四个参数据分别表示为:x、y、width、heigth,(40、112)表示pictureBox1的Lcation的坐标,(120、144)表示pictureBox1控件的宽度和高度
this.pictureBox1.Image = IC.KiCut1((Bitmap)(this.GetSelectImage(this.pictureBox1.Width, this.pictureBox1.Height))); //(120、144)表示pictureBox1控件的宽度和高度
//this.pictureBox1.Image = (this.GetSelectImage(120, 144));
}
catch (Exception ex)
{
MessageBox.Show("格式不对");
ex.ToString();
}
}
}
#endregion
#region 定义显示图像方法,即将打开的图像在pictureBox1控件显示
public void Show(string strHeadImagePath)
{
this.pictureBox1.Load(@strHeadImagePath); //
}
#endregion
#region 获取图像
/// <summary>
/// 获取指定宽度和高度的图像即使图片和pictureBox1控件一样宽和高,返回值为图片Image
/// </summary>
/// <param name="Width表示宽"></param>
/// <param name="Height表示高"></param>
/// <returns></returns>
public Image GetSelectImage(int Width, int Height)
{
//Image initImage = this.pictureBox1.Image;
Image initImage = Bi;
//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
//原始图片的宽、高
int initWidth = initImage.Width;
int initHeight = initImage.Height; //非正方型先裁剪为正方型
if (initWidth != initHeight)
{
//截图对象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null; //宽大于高的横图
if (initWidth > initHeight)
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle((initWidth - initHeight) / , , initHeight, initHeight);
Rectangle toR = new Rectangle(, , initHeight, initHeight);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置宽
initWidth = initHeight;
}
//高大于宽的竖图
else
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle(, (initHeight - initWidth) / , initWidth, initWidth);
Rectangle toR = new Rectangle(, , initWidth, initWidth);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置高
initHeight = initWidth;
} initImage = (System.Drawing.Image)pickedImage.Clone();
// //释放截图资源
pickedG.Dispose();
pickedImage.Dispose();
} return initImage;
}
}
#endregion
#region 点击button2按钮事件
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = "裁剪后的图片宽度:"+this.pictureBox2.Width.ToString();
this.label2.Text = "裁剪后的图片高度:"+this.pictureBox2.Height.ToString();
}
#endregion
#region 缩放、裁剪图像用到的变量
/// <summary>
///
/// </summary>
int x1; //鼠标按下时横坐标
int y1; //鼠标按下时纵坐标
int width; //所打开的图像的宽
int heigth; //所打开的图像的高
bool HeadImageBool = false; // 此布尔变量用来判断pictureBox1控件是否有图片
#endregion
#region 画矩形使用到的变量
Point p1; //定义鼠标按下时的坐标点
Point p2; //定义移动鼠标时的坐标点
Point p3; //定义松开鼠标时的坐标点
#endregion
#region 鼠标按下时发生的事件
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;
this.p1 = new Point(e.X, e.Y);
x1 = e.X;
y1 = e.Y;
if (this.pictureBox1.Image != null)
{
HeadImageBool = true;
}
else
{
HeadImageBool = false;
}
}
#endregion
#region 移动鼠标发生的事件
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.Cursor == Cursors.Cross)
{
this.p2 = new Point(e.X, e.Y);
if ((p2.X - p1.X) > && (p2.Y - p1.Y) > ) //当鼠标从左上角向开始移动时P3坐标
{
this.p3 = new Point(p1.X, p1.Y);
}
if ((p2.X - p1.X) < && (p2.Y - p1.Y) > ) //当鼠标从右上角向左下方向开始移动时P3坐标
{
this.p3 = new Point(p2.X, p1.Y);
}
if ((p2.X - p1.X) > && (p2.Y - p1.Y) < ) //当鼠标从左下角向上开始移动时P3坐标
{
this.p3 = new Point(p1.X, p2.Y);
}
if ((p2.X - p1.X) < && (p2.Y - p1.Y) < ) //当鼠标从右下角向左方向上开始移动时P3坐标
{
this.p3 = new Point(p2.X, p2.Y);
}
this.pictureBox1.Invalidate(); //使控件的整个图面无效,并导致重绘控件
}
}
#endregion
#region 松开鼠标发生的事件,实例化ImageCut1类对像
ImageCut1 IC1; //定义所画矩形的图像对像
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (HeadImageBool)
{
width = this.pictureBox1.Image.Width;
heigth = this.pictureBox1.Image.Height;
if ((e.X - x1) > && (e.Y - y1) > ) //当鼠标从左上角向右下方向开始移动时发生
{
IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
if ((e.X - x1) < && (e.Y - y1) > ) //当鼠标从右上角向左下方向开始移动时发生
{
IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
if ((e.X - x1) > && (e.Y - y1) < ) //当鼠标从左下角向右上方向开始移动时发生
{
IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
if ((e.X - x1) < && (e.Y - y1) < ) //当鼠标从右下角向左上方向开始移动时发生
{
IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
}
this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width;
this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height;
this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
this.Cursor = Cursors.Default;
}
else
{
this.Cursor = Cursors.Default;
}
}
#endregion
#region 获取所选矩形图像
/// <summary>
///
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <returns></returns>
public Image GetSelectImage1(int Width, int Height)
{
Image initImage = this.pictureBox1.Image;
//Image initImage = Bi;
//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
//原始图片的宽、高
int initWidth = initImage.Width;
int initHeight = initImage.Height; //非正方型先裁剪为正方型
if (initWidth != initHeight)
{
//截图对象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null; //宽大于高的横图
if (initWidth > initHeight)
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle((initWidth - initHeight) / , , initHeight, initHeight);
Rectangle toR = new Rectangle(, , initHeight, initHeight);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置宽
initWidth = initHeight;
}
//高大于宽的竖图
else
{
//对象实例化
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//定位
Rectangle fromR = new Rectangle(, (initHeight - initWidth) / , initWidth, initWidth);
Rectangle toR = new Rectangle(, , initWidth, initWidth);
//画图
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//重置高
initHeight = initWidth;
} initImage = (System.Drawing.Image)pickedImage.Clone();
// //释放截图资源
pickedG.Dispose();
pickedImage.Dispose();
} return initImage;
}
}
#endregion
#region 重新绘制pictureBox1控件,即移动鼠标画矩形
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (HeadImageBool)
{
Pen p = new Pen(Color.Black, );//画笔
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//Bitmap bitmap = new Bitmap(strHeadImagePath);
Bitmap bitmap = Bi;
Rectangle rect = new Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y)));
e.Graphics.DrawRectangle(p, rect);
}
else
{ }
}
#endregion
}
}

第二部分是辅助方法类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ImageClassLib
{
public class ImageCut1
{
#region 剪裁图片方法
/// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap,即需要裁剪的图片</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public Bitmap KiCut1(Bitmap b)
{
if (b == null)
{
return null;
} int w = b.Width;
int h = b.Height; if (X >= w || Y >= h)
{
return null;
} if (X + Width > w)
{
Width = w - X;
} if (Y + Height > h)
{
Height = h - Y;
} try
{
Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmpOut);
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle(, , Width, Height); //所画的矩形正确,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。 // Create rectangle for source image.
Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect参数指定要绘制的 image 对象的矩形部分。 将此部分进行缩放以适合 destRect 参数所指定的矩形。 g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel);
//resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
#endregion
#region ImageCut1类的构造函数
public int X;
public int Y;
public int Width ;
public int Height;
/// <summary>
/// ImageCut1类的构造函数,ImageCut1类用来获取鼠标在pictureBox1控件所画矩形内的图像
/// </summary>
/// <param name="x表示鼠标在pictureBox1控件上按下时的横坐标"></param>
/// <param name="y表示鼠标在pictureBox1控件上按下时的纵坐标"></param>
/// <param name="width表示鼠标在pictureBox1控件上松开鼠标的宽度"></param>
/// <param name="heigth表示鼠标在pictureBox1控件上松开鼠标的高度"></param>
public ImageCut1(int x, int y, int width, int heigth)
{
X = x;
Y = y;
Width = width;
Height = heigth;
}
#endregion
}
}

实现的效果如下:

C# 实现QQ式截图功能的更多相关文章

  1. mac qq截图功能失效后,如何重启截图功能?

    在finder中打开应用程序目录,找到QQ,右键单击QQ,选择显示包内容,此时会打开一个文件夹. 进入以下路径Library/LoginItems然后双击ScreenCapture这个进程,截图功能即 ...

  2. 实现能够直接粘QQ贴截图的bug管理功能

    对于一个功能强大的协作平台来说,todo管理和bug管理是不可缺少的功能.Todo和bug往往不是通过一些简单的文字就能实现的,有时候须要配以图片的说名,之前用过的项目管理平台都是以附件的形式上传图片 ...

  3. WPF C#截图功能 仿qq截图

    原文:WPF C#截图功能 仿qq截图 先上效果图 源码下载地址:http://download.csdn.net/detail/candyvoice/9788099 描述:启动程序,点击窗口butt ...

  4. C#软件开发实例.个人定制自己的屏幕抓图工具(八)加入了截图功能键盘

    章文件夹 (一)功能概览 (二)创建项目.注冊热键.显示截图主窗体 (三)托盘图标及菜单的实现 (四)基本截图功能实现 (五)针对拖拽时闪烁卡顿现象的优化 (六)加入配置管理功能 (七)加入放大镜的功 ...

  5. canvas与html5实现视频截图功能

    这段时间一直在研究canvas,突发奇想想做一个可以截屏视频的功能,然后把图片拉去做表情包,哈哈哈哈哈哈~~ 制作方法: 1.在页面中加载视频 在使用canvas制作这个截图功能时,首先必须保证页面上 ...

  6. 用JQuery仿造QQ头像裁剪功能

    最近工作真心忙碌,几乎没时间写博客.今天趁周末来仿一个QQ头像裁剪功能插件.效果如下: 所有文件都可在我的Github上下载,从头到尾从简到繁按步骤一共分了9个HTML文件,每个步骤文件里的注释都写的 ...

  7. [课程设计]Scrum 2.6 多鱼点餐系统开发进度(下单一览页面-菜式添加功能实现)

    Scrum 2.6 多鱼点餐系统开发进度  (下单一览页面-菜式添加功能实现) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题 ...

  8. [课程设计]Scrum 2.7 多鱼点餐系统开发进度(下单一览页面-菜式添加功能的继续实现)

    Scrum 2.7 多鱼点餐系统开发进度  (下单一览页面-菜式添加功能的继续实现) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团 ...

  9. [课程设计]Scrum 2.8 多鱼点餐系统开发进度(下单一览页面-菜式一览功能的最终实现)

    Scrum 2.8 多鱼点餐系统开发进度 (下单一览页面-菜式一览功能的最终实现) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队 ...

随机推荐

  1. ACM-ICPC 2018 南京赛区网络预赛 - L Magical Girl Haze (分层迪杰斯特拉)

    题意:N个点,M条带权有向边,求可以免费K条边权值的情况下,从点1到点N的最短路. 分析:K<=10,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费.在迪杰斯特拉的dfs过程中 ...

  2. [转]Ubuntu使用Wireshark找不到interface的解决方法

    Wireshark是一款强大的有图形界面的网络封包分析工具. dumpcap需要root权限才能使用的,以普通用户打开Wireshark,Wireshark当然没有权限使用dumpcap进行截取封包. ...

  3. 关于ajax中responseText不能返回脚本的问题。

    今天做后台时想用ajax返回带有脚本的HTML DOM ,发现脚本被当成字符串了,查了一下responseText()方法返回的是字符串,查了一下百度,用了正则匹配,匹配到脚本然后执行,不想当一个伸手 ...

  4. 337APuzzles

    dangerous /*大水题目.不解释 给你m个数,从中选出n个,保证最大值和最小值的差值最小, 做法:从小到大排序,然后暴力枚举每个长度是n的序列*/ #include<stdio.h> ...

  5. Activiti:创建activiti工程

    Activiti:创建activiti工程 一.Activiti下载: 1,Activiti下载地址:https://github.com/Activiti/Activiti/releases 2,A ...

  6. Linux Shell脚本编程--字符串截取

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} ...

  7. 20145310《Java程序设计》第4次实验报告

    20145310<Java程序设计>第4次实验报告 实验内容 搭建Android环境 运行Android 修改代码并输出自己的学号 实验步骤 搭建Android环境 安装Android S ...

  8. 20145312 《Java程序设计》第九周学习总结

    20145312 <Java程序设计>第九周学习总结 学习笔记 Chapter 16整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 SUN公司为了简化.统一对数据库的操作, ...

  9. VMware 共享目录不显示的解决办法

    centos mount -t vmhgfs .host:/ /mnt/hgfs # 如果有如下提示: # Error: cannot mount filesystem: No such device ...

  10. PHP的memory_limit引起的问题

    在运行PHP程序,通常会遇到下面的错误, 这个意味着PHP脚本使用了过多的内存,并超出了系统对其设置的允许最大内存.解决这个问题,首先需要查看你的程序是否分配了过多的内存,在程序没有问题的情况下,你可 ...