图片截剪

    public class ImageUtil{#region " 正方型裁剪 "/// <summary>  /// 正方型裁剪  /// 以图片中心为轴心,截取正方型,然后等比缩放  /// 用于头像处理  /// </summary> /// <param name="postedFile">原图HttpPostedFile对象</param>  /// <param name="fileSaveUrl">缩略图存放地址</param>  /// <param name="side">指定的边长(正方型)</param>  /// <param name="quality">质量(范围0-100)</param>  public static void CutForSquare(string fromFilePath, string fileSaveUrl, int side, int quality){//创建目录  string dir = Path.GetDirectoryName(fileSaveUrl);if (!Directory.Exists(dir))Directory.CreateDirectory(dir);//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)  System.Drawing.Image initImage = System.Drawing.Image.FromFile(fromFilePath, true);//原图宽高均小于模版,不作处理,直接保存  if (initImage.Width <= side && initImage.Height <= side){initImage.Save(fileSaveUrl);}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) / 2, 0, initHeight, initHeight);Rectangle toR = new Rectangle(0, 0, 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(0, (initHeight - initWidth) / 2, initWidth, initWidth);Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);//画图  pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);//重置高  initHeight = initWidth;}                                               //将截图对象赋给原图  initImage = (System.Drawing.Image)pickedImage.Clone();//释放截图资源  pickedG.Dispose();pickedImage.Dispose();}//缩略图对象  System.Drawing.Image resultImage = new System.Drawing.Bitmap(side, side);System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage);//设置质量  resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//用指定背景色清空画布  resultG.Clear(Color.White);//绘制缩略图  resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);//关键质量控制  //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff  ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();ImageCodecInfo ici = null;foreach (ImageCodecInfo i in icis){if (i.FilenameExtension.Contains(fileSaveUrl.Substring(fileSaveUrl.LastIndexOf('.'), fileSaveUrl.Length - fileSaveUrl.LastIndexOf('.')).ToUpper())){ici = i;}}EncoderParameters ep = new EncoderParameters(1);ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);//保存缩略图  resultImage.Save(fileSaveUrl, ici, ep);//释放关键质量控制所用资源  ep.Dispose();//释放缩略图资源                          resultG.Dispose();resultImage.Dispose();//释放原始图片资源  initImage.Dispose();}}#endregion#region " 固定模版裁剪并缩放 "/// <summary>  /// 指定长宽裁剪  /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸  /// </summary>  /// <param name="postedFile">原图HttpPostedFile对象</param>  /// <param name="fileSaveUrl">保存路径</param>  /// <param name="maxWidth">最大宽(单位:px)</param>  /// <param name="maxHeight">最大高(单位:px)</param>  /// <param name="quality">质量(范围0-100)</param>  public static void CutForCustom(string fromFilePath, string fileSaveUrl, int maxWidth, int maxHeight, int quality){string dir = Path.GetDirectoryName(fileSaveUrl);if (!Directory.Exists(dir))Directory.CreateDirectory(dir);//从文件获取原始图片,并使用流中嵌入的颜色管理信息  System.Drawing.Image initImage = System.Drawing.Image.FromFile(fromFilePath, true);//原图宽高均小于模版,不作处理,直接保存  if (initImage.Width <= maxWidth && initImage.Height <= maxHeight){initImage.Save(fileSaveUrl);}else{//模版的宽高比例  double templateRate = (double)maxWidth / maxHeight;//原图片的宽高比例  double initRate = (double)initImage.Width / initImage.Height;//原图与模版比例相等,直接缩放  if (templateRate == initRate){//按模版大小生成最终图片  System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;templateG.Clear(Color.White);templateG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, maxWidth, maxHeight), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);}//原图与模版比例不等,裁剪后缩放  else{//裁剪对象  System.Drawing.Image pickedImage = null;System.Drawing.Graphics pickedG = null;//定位  Rectangle fromR = new Rectangle(0, 0, 0, 0);//原图裁剪定位  Rectangle toR = new Rectangle(0, 0, 0, 0);//目标定位  //宽为标准进行裁剪  if (templateRate > initRate){//裁剪对象实例化  pickedImage = new System.Drawing.Bitmap(initImage.Width, (int)Math.Floor(initImage.Width / templateRate));pickedG = System.Drawing.Graphics.FromImage(pickedImage);//裁剪源定位  fromR.X = 0;fromR.Y = (int)Math.Floor((initImage.Height - initImage.Width / templateRate) / 2);fromR.Width = initImage.Width;fromR.Height = (int)Math.Floor(initImage.Width / templateRate);//裁剪目标定位  toR.X = 0;toR.Y = 0;toR.Width = initImage.Width;toR.Height = (int)Math.Floor(initImage.Width / templateRate);}//高为标准进行裁剪  else{pickedImage = new System.Drawing.Bitmap((int)Math.Floor(initImage.Height * templateRate), initImage.Height);pickedG = System.Drawing.Graphics.FromImage(pickedImage);fromR.X = (int)Math.Floor((initImage.Width - initImage.Height * templateRate) / 2);fromR.Y = 0;fromR.Width = (int)Math.Floor(initImage.Height * templateRate);fromR.Height = initImage.Height;toR.X = 0;toR.Y = 0;toR.Width = (int)Math.Floor(initImage.Height * templateRate);toR.Height = initImage.Height;}//设置质量  pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//裁剪  pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);//按模版大小生成最终图片  System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;templateG.Clear(Color.White);templateG.DrawImage(pickedImage, new System.Drawing.Rectangle(0, 0, maxWidth, maxHeight), new System.Drawing.Rectangle(0, 0, pickedImage.Width, pickedImage.Height), System.Drawing.GraphicsUnit.Pixel);//关键质量控制  //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff  ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();ImageCodecInfo ici = null;foreach (ImageCodecInfo i in icis){if (i.FilenameExtension.Contains(fileSaveUrl.Substring(fileSaveUrl.LastIndexOf('.'), fileSaveUrl.Length - fileSaveUrl.LastIndexOf('.')).ToUpper())){ici = i;}}EncoderParameters ep = new EncoderParameters(1);ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);//保存缩略图  templateImage.Save(fileSaveUrl, ici, ep);//templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);  //释放资源  templateG.Dispose();templateImage.Dispose();pickedG.Dispose();pickedImage.Dispose();}}//释放资源  initImage.Dispose();}#endregion}

  

转载于:https://www.cnblogs.com/bober/archive/2013/03/14/2958768.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/576067.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

普通用户的sudo权限,禁止root用户登录

假如增加用户zhangsan的sudo权限。 修改/etc/sudoers文件&#xff0c;在root下增加如下一行。 增加用户名zhangsan&#xff1a; 禁止root用户登录&#xff1a; 修改/etc/ssh/sshd_config文件&#xff0c; 将 PermitRootLogin前的#号去掉&#xff0c;yes改为no&#xff0c;重启n…

C语言system函数用法

system()函数用于向操作系统传递控制台命令行&#xff0c;以WINDOWS系统为例&#xff0c;通过system()函数执行命令和在DOS窗口中执行命令的效果是一样的&#xff0c;所以只要在运行窗口中可以使用的命令都可以用SYSTEM&#xff08;&#xff09;传递&#xff0c;但要注意的是输…

什么是 MVC ?

ylbtech-Architecture: MVCASP.NET中 MVC。 1.A,概念 MVC是一种目前广泛流行的软件设计模式&#xff0c;MVC英文即Model-View-Controller&#xff0c;即把一个应用的输入、处理、输出 流程按照Model、View、Controller的方式进行分离&#xff0c;这样一个应用被分成三 个层——…

zabbix邮件报警

原文出自http://www.iyunv.com/thread-22904-1-1.html 首先web端的配置顺序如下&#xff1a; 创建用户媒介-->创建用户组和用户-->针对trigger&#xff08;触发器&#xff09;添加报警动作&#xff0c;设置邮件发送用户及媒介1. 创建用户媒介创建用户媒介-->创建用户…

ubuntu下定时执行工具cron开启关闭重启

配置文件一般为/etc/init.d/cron 启动&#xff1a;sudo /etc/init.d/cron start 关闭&#xff1a;sudo /etc/init.d/cron stop 重启&#xff1a;sudo /etc/init.d/cron restart 重新载入配置&#xff1a;sudo /etc/init.d/cron reload 可以用ps aux | grep cron命令查看cron是否…

Linux运维学习大纲

1、linux系统基础&#xff0c;这个不用说了&#xff0c;是基础中的基础&#xff0c;连这个都不会就别干了&#xff0c;参考书籍&#xff0c;可以看鸟哥linux基础篇&#xff0c;至少要掌握这书60%内容&#xff0c;没必须全部掌握&#xff0c;但基本命令总得会吧2、网络服务&…

zabbix的入门到精通之zabbix的触发器Trigger

第1章 Trigger 1.1 创建一个trigger选择&#xff1a;ConfigurationHost双击: Trigger双击:Create Trigger(位置在右上角)后图下图所示NameTrigger的名字Expression添加Trigger表达式&#xff0c;双击add后添加Multiple PROBLEM events generationDescription对trigger的描述URL…

Zabbix 探索主机 “Discovery” 自动发现主机 详细图文教程

Zabbix 自动发现&#xff08;Discovery&#xff09;功能使用随着 监控 主机不断增多&#xff0c;有的时候需要添加一批机器&#xff0c;特别是刚用zabbix的童鞋 需要将公司的所有服务器添加到zabbix&#xff0c;如果使用传统办法去单个添加设备、分组、项目、图像…..结果应该是…

图解如何制作苹果OS X系统ISO光盘

当我们从网上下载的原版苹果系统是DMG格式的&#xff0c;要做系统引导必须做成ISO才能做系统盘&#xff0c;所以本文介绍在Windows7下如何来制作苹果系统光盘&#xff0c;各位黑苹果的童鞋要注意了。 准备以下三个东西。 1、苹果OS10.8种子下载 OS X 10.8 正式版种子.torrent 2…

zabbix的b编译安装

原文出自&#xff1a;http://www.ttlsa.com/zabbix/install-zabbix-on-linux-5-ttlsa/ 在了解《zabbix硬件、软件需求》之后&#xff0c;在你心里应该有备选的机器。今天开始安装zabbix。zabbix需要LNMP或者LAMP环境。环境的搭建不在本章范围内。 LNMP环境配置 Linux安装&#…

不大于数的2整数幂的数

获得不大于数的2整数幂的数。例如&#xff0c; 不大于6的2整数幂的数是4. 1 #include <bitset>2 3 using namespace std;4 5 /*6 * 返回不大于num的最大数的2进制数幂次。7 */8 int GetMaxPos(int num)9 { 10 int flag num & (num - 1); 11 12 if (flag …

kangle web server源代码安装简明教程

原文出自https://www.kanglesoft.com/thread-6001-1-1.html 首先到kangle官方网站上下载最新的源代码。 前提条件&#xff1a; 请先确保你的系统上有g,libz开发包,libpcre开发包,libiconv开发包(非linux版要).如你的系统为centos/rhel则运行下面命令安装这些包: yum -y ins…

关闭应用程序的几种方法

Application.Exit();//注意Application在using System.Windows.Forms命名空间中; System.Diagnostics.Process.GetCurrentProcess().Kill(); 转载于:https://www.cnblogs.com/ganquanfu2008/archive/2013/03/26/2982609.html

mysql5.6 二进制免编译安装

原文出自http://www.ttlsa.com/mysql/install-mysql5_6/ 1. 安装必要的组件 yum install –y autoconf automake imake libxml2-devel\expat-devel cmake gcc gcc-clibaio libaio-devel bzr bison libtool ncurses5-devel2. 下载解压mysql软件 2345# cd /usr/local/src# wget …

github上的优秀项目和开发环境配置【转http://www.cnblogs.com/2018/archive/2012/11/09/2763119.html】...

github上的优秀项目和开发环境配置 国外的几个公司开放的资源 https://github.com/google https://github.com/facebook https://github.com/joyent node.jshttps://github.com/jquery https://github.com/torvalds linux系统 http://twitter.github.com/ Bootstrap 是很流…

zabbix agent 类型所有key

原文转自&#xff1a;http://www.ttlsa.com/zabbix/zabbix-agent-types-and-all-keys/ zabbix服务器端通过与zabbix agent通信来获取客户端服务器的数据&#xff0c;agent分为两个版本&#xff0c;其中一个是主动一个是被动&#xff0c;在配置主机我们可以看到一个是agent&…

FFmpeg常见命令行

1、ffmpeg命令行 视频生成图片 ffmpeg -i test.mp4 -r 25 -f image2 data/image%3d.jpg这个命令行使用FFmpeg工具将视频文件&#xff08;test.mp4&#xff09;转换为一系列图像文件。 让我们逐个解释每个参数的含义&#xff1a; -i test.mp4: 指定输入文件为test.mp4。-i是F…

NetAdvangate Infragisticss 控件在工程移动到别的机器上,引用失效问题

1.这是一个Bug。因为其他控件&#xff0c;比如DevExpress不存在这个问题。 2.解决的方法也很简单&#xff0c;先把无法找到的引用记录下来&#xff0c;然后把这些应用删除&#xff0c;最后重新把它们引用进来就行了。不过&#xff0c;这样一来&#xff0c;工程每次移动到别的机…

sharepoint被阻止的文件类型解释说明

可限制上载或下载特定文件类型。每个 Web 应用程序都维护一个基于文件扩展名的阻止文件类型列表。例如&#xff0c;由于扩展名为 .exe 的文件可能会在客户端计算机上运行并可能包含恶意代码&#xff0c;因此可阻止此类文件。 默认情况下&#xff0c;会阻止许多文件类型&#xf…

zabbix 监控使用宏自动发现网卡并进行监控

一、自动发现网卡并进行监控 首先新建一个模板 1、探索规则名称为Network interface discovery 键值为net.if.discovery 2、filter处要把宏给添加上{#IFNAME} matches Network interfaces for discovery 3、新建项目原型 创建网卡进来的流量的监控项 Incoming network…