C#图片处理基本应用(裁剪,缩放,清晰度,水印)

C#图片处理基本应用(裁剪,缩放,清晰度,水印)

吴剑

http://wu-jian.cnblogs.com/

前言

需求源自项目中的一些应用,比如相册功能,通常用户上传相片后我们都会针对该相片再生成一张缩略图,用于其它页面上的列表显示。随便看一下,大部分网站基本都是将原图等比缩放来生成缩略图。但完美主义者会发现一些问题,比如显示排版时想让相片缩略图列表非常统一、整齐、和美观,比如要求每张缩略图大小固定为120 x 90且不拉伸变形怎么办?再比如用户头像如何让缩略图比原图更清晰?或是如何在上传的图片下加一个半透明的LOGO水印?

OK,本文根据自己的项目代码描述以上问题的解决方案,全部基于.Net Framework类库完成,代码中包含了C#图片处理的一些基础知识,与大家分享,个人能力有限,不足之处还请及时指正。

提高缩略图清晰度

(原图200*200,12.3k)(处理后80*80,17.7k)

之前一直认为缩略图不可能比原图清晰,直到某天一位产品的同事给我看某网站的效果。于是开始寻找.NET下实现代码,仔细观察缩略图确实比原图更清晰了一些,但代价是缩略图文件比原图更大,所以如果你想让一张占满显示器屏幕的超大图片更清晰,那么图片占用空间和网络流量就必需考虑了,如果是互联网应用,建议缩略图在200像素以内的使用该方法。当然如果哪位有更好的代码即能让图片文件大小变化不大又让图片更清晰还请分享。

图片裁剪

(原图256*192)(裁剪要求100*100)

(原图256*192)(裁剪要求90*120)

(原图256*192)(裁剪要求120*90)

(原图146*256)(裁剪要求100*100)

(原图146*256)(裁剪要求90*120)

(原图146*256)(裁剪要求120*90)

算法:以原图中心作为裁剪中心,最大范围的对原图进行裁剪,然后对裁剪结果等比缩放。

图片水印

仅演示了效果,如需要变更字体、水印透明度、位置等可自行在代码或方法中扩展。

代码

封装了几个通用的方法,如发现有BUG或漏洞还请及时指正。

0001 using System;
0002 using System.Collections.Generic;
0003 using System.Text;
0004 using System.IO;
0005 using System.Drawing;
0006 using System.Drawing.Drawing2D;
0007 using System.Drawing.Imaging;
0008 namespace WuJian.Common
0009 {
0010     /// 
0011     /// 图片处理类
0012     /// 吴剑 2008-07-02 创建
0013     /// 吴剑 2011-01-21 修改
0014     /// 
0015     public class PTImage
0016     {
0017         #region 正方型裁剪并缩放
0018         /// 
0019         /// 正方型裁剪
0020         /// 以图片中心为轴心,截取正方型,然后等比缩放
0021         /// 用于头像处理
0022         /// 
0023         /// 
  
  
   
   吴剑 2010-11-23
  
  
0024         /// 原图HttpPostedFile对象
0025         /// 缩略图存放地址
0026         /// 指定的边长(正方型)
0027         /// 质量(范围0-100)
0028         public static void CutForSquare(System.Web.HttpPostedFile postedFile, string fileSaveUrl, int side, int quality)
0029         {
0030             //创建目录
0031             string dir = Path.GetDirectoryName(fileSaveUrl);
0032             if (!Directory.Exists(dir))
0033                 Directory.CreateDirectory(dir);
0034             //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
0035             System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);
0036             //原图宽高均小于模版,不作处理,直接保存
0037             if (initImage.Width <= side && initImage.Height <= side)
0038             {
0039                 initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
0040             }
0041             else
0042             {
0043                 //原始图片的宽、高
0044                 int initWidth = initImage.Width;
0045                 int initHeight = initImage.Height;
0046                 //非正方型先裁剪为正方型
0047                 if (initWidth != initHeight)
0048                 {
0049                     //截图对象
0050                     System.Drawing.Image pickedImage = null;
0051                     System.Drawing.Graphics pickedG = null;
0052                     //宽大于高的横图
0053                     if (initWidth > initHeight)
0054                     {
0055                         //对象实例化
0056                         pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
0057                         pickedG = System.Drawing.Graphics.FromImage(pickedImage);
0058                         //设置质量
0059                         pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0060                         pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0061                         //定位
0062                         Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
0063                         Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
0064                         //画图
0065                         pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
0066                         //重置宽
0067                         initWidth = initHeight;
0068                     }
0069                     //高大于宽的竖图
0070                     else
0071                     {
0072                         //对象实例化
0073                         pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
0074                         pickedG = System.Drawing.Graphics.FromImage(pickedImage);
0075                         //设置质量
0076                         pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0077                         pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0078                         //定位
0079                         Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
0080                         Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
0081                         //画图
0082                         pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
0083                         //重置高
0084                         initHeight = initWidth;
0085                     }
0086                     //将截图对象赋给原图
0087                     initImage = (System.Drawing.Image)pickedImage.Clone();
0088                     //释放截图资源
0089                     pickedG.Dispose();
0090                     pickedImage.Dispose();
0091                 }
0092                 //缩略图对象
0093                 System.Drawing.Image resultImage = new System.Drawing.Bitmap(side, side);
0094                 System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage);
0095                 //设置质量
0096                 resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0097                 resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0098                 //用指定背景色清空画布
0099                 resultG.Clear(Color.White);
0100                 //绘制缩略图
0101                 resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
0102                 //关键质量控制
0103                 //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
0104                 ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
0105                 ImageCodecInfo ici = null;
0106                 foreach (ImageCodecInfo i in icis)
0107                 {
0108                     if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
0109                     {
0110                         ici = i;
0111                     }
0112                 }
0113                 EncoderParameters ep = new EncoderParameters(1);
0114                 ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
0115                 //保存缩略图
0116                 resultImage.Save(fileSaveUrl, ici, ep);
0117                 //释放关键质量控制所用资源
0118                 ep.Dispose();
0119                 //释放缩略图资源
0120                 resultG.Dispose();
0121                 resultImage.Dispose();
0122                 //释放原始图片资源
0123                 initImage.Dispose();
0124             }
0125         }
0126         /// 
0127         /// 正方型裁剪
0128         /// 以图片中心为轴心,截取正方型,然后等比缩放
0129         /// 用于头像处理
0130         /// 
0131         /// 
  
  
   
   吴剑 2010-11-23
  
  
0132         /// 原图HttpPostedFile对象
0133         /// 缩略图存放地址
0134         /// 指定的边长(正方型)
0135         /// 质量(范围0-100)
0136         public static void CutForSquare(System.IO.Stream fromFile, string fileSaveUrl, int side, int quality)
0137         {
0138             //创建目录
0139             string dir = Path.GetDirectoryName(fileSaveUrl);
0140             if (!Directory.Exists(dir))
0141                 Directory.CreateDirectory(dir);
0142             //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
0143             System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
0144             //原图宽高均小于模版,不作处理,直接保存
0145             if (initImage.Width <= side && initImage.Height <= side)
0146             {
0147                 initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
0148             }
0149             else
0150             {
0151                 //原始图片的宽、高
0152                 int initWidth = initImage.Width;
0153                 int initHeight = initImage.Height;
0154                 //非正方型先裁剪为正方型
0155                 if (initWidth != initHeight)
0156                 {
0157                     //截图对象
0158                     System.Drawing.Image pickedImage = null;
0159                     System.Drawing.Graphics pickedG = null;
0160                     //宽大于高的横图
0161                     if (initWidth > initHeight)
0162                     {
0163                         //对象实例化
0164                         pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
0165                         pickedG = System.Drawing.Graphics.FromImage(pickedImage);
0166                         //设置质量
0167                         pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0168                         pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0169                         //定位
0170                         Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
0171                         Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
0172                         //画图
0173                         pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
0174                         //重置宽
0175                         initWidth = initHeight;
0176                     }
0177                     //高大于宽的竖图
0178                     else
0179                     {
0180                         //对象实例化
0181                         pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
0182                         pickedG = System.Drawing.Graphics.FromImage(pickedImage);
0183                         //设置质量
0184                         pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0185                         pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0186                         //定位
0187                         Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
0188                         Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
0189                         //画图
0190                         pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
0191                         //重置高
0192                         initHeight = initWidth;
0193                     }
0194                     //将截图对象赋给原图
0195                     initImage = (System.Drawing.Image)pickedImage.Clone();
0196                     //释放截图资源
0197                     pickedG.Dispose();
0198                     pickedImage.Dispose();
0199                 }
0200                 //缩略图对象
0201                 System.Drawing.Image resultImage = new System.Drawing.Bitmap(side, side);
0202                 System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage);
0203                 //设置质量
0204                 resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0205                 resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0206                 //用指定背景色清空画布
0207                 resultG.Clear(Color.White);
0208                 //绘制缩略图
0209                 resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
0210                 //关键质量控制
0211                 //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
0212                 ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
0213                 ImageCodecInfo ici = null;
0214                 foreach (ImageCodecInfo i in icis)
0215                 {
0216                     if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
0217                     {
0218                         ici = i;
0219                     }
0220                 }
0221                 EncoderParameters ep = new EncoderParameters(1);
0222                 ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
0223                 //保存缩略图
0224                 resultImage.Save(fileSaveUrl, ici, ep);
0225                 //释放关键质量控制所用资源
0226                 ep.Dispose();
0227                 //释放缩略图资源
0228                 resultG.Dispose();
0229                 resultImage.Dispose();
0230                 //释放原始图片资源
0231                 initImage.Dispose();
0232             }
0233         }
0234         #endregion
0235         #region 固定模版裁剪并缩放
0236         /// 
0237         /// 指定长宽裁剪
0238         /// 按模版比例最大范围的裁剪图片并缩放至模版尺寸
0239         /// 
0240         /// 
  
  
   
   吴剑 2010-11-15
  
  
0241         /// 原图HttpPostedFile对象
0242         /// 保存路径
0243         /// 最大宽(单位:px)
0244         /// 最大高(单位:px)
0245         /// 质量(范围0-100)
0246         public static void CutForCustom(System.Web.HttpPostedFile postedFile, string fileSaveUrl, int maxWidth, int maxHeight, int quality)
0247         {
0248             //从文件获取原始图片,并使用流中嵌入的颜色管理信息
0249             System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);
0250             //原图宽高均小于模版,不作处理,直接保存
0251             if (initImage.Width <= maxWidth && initImage.Height <= maxHeight)
0252             {
0253                 initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
0254             }
0255             else
0256             {
0257                 //模版的宽高比例
0258                 double templateRate = (double)maxWidth / maxHeight;
0259                 //原图片的宽高比例
0260                 double initRate = (double)initImage.Width / initImage.Height;
0261                 //原图与模版比例相等,直接缩放
0262                 if (templateRate == initRate)
0263                 {
0264                     //按模版大小生成最终图片
0265                     System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);
0266                     System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
0267                     templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
0268                     templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0269                     templateG.Clear(Color.White);
0270                     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);
0271                     templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
0272                 }
0273                 //原图与模版比例不等,裁剪后缩放
0274                 else
0275                 {
0276                     //裁剪对象
0277                     System.Drawing.Image pickedImage = null;
0278                     System.Drawing.Graphics pickedG = null;
0279                     //定位
0280                     Rectangle fromR = new Rectangle(0, 0, 0, 0);//原图裁剪定位
0281                     Rectangle toR = new Rectangle(0, 0, 0, 0);//目标定位
0282                     //宽为标准进行裁剪
0283                     if (templateRate > initRate)
0284                     {
0285                         //裁剪对象实例化
0286                         pickedImage = new System.Drawing.Bitmap(initImage.Width, (int)Math.Floor(initImage.Width / templateRate));
0287                         pickedG = System.Drawing.Graphics.FromImage(pickedImage);
0288                         //裁剪源定位
0289                         fromR.X = 0;
0290                         fromR.Y = (int)Math.Floor((initImage.Height - initImage.Width / templateRate) / 2);
0291                         fromR.Width = initImage.Width;
0292                         fromR.Height = (int)Math.Floor(initImage.Width / templateRate);
0293                         //裁剪目标定位
0294                         toR.X = 0;
0295                         toR.Y = 0;
0296                         toR.Width = initImage.Width;
0297                         toR.Height = (int)Math.Floor(initImage.Width / templateRate);
0298                     }
0299                     //高为标准进行裁剪
0300                     else
0301                     {
0302                         pickedImage = new System.Drawing.Bitmap((int)Math.Floor(initImage.Height * templateRate), initImage.Height);
0303                         pickedG = System.Drawing.Graphics.FromImage(pickedImage);
0304                         fromR.X = (int)Math.Floor((initImage.Width - initImage.Height * templateRate) / 2);
0305                         fromR.Y = 0;
0306                         fromR.Width = (int)Math.Floor(initImage.Height * templateRate);
0307                         fromR.Height = initImage.Height;
0308                         toR.X = 0;
0309                         toR.Y = 0;
0310                         toR.Width = (int)Math.Floor(initImage.Height * templateRate);
0311                         toR.Height = initImage.Height;
0312                     }
0313                     //设置质量
0314                     pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0315                     pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0316                     //裁剪
0317                     pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
0318                     //按模版大小生成最终图片
0319                     System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);
0320                     System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
0321                     templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
0322                     templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0323                     templateG.Clear(Color.White);
0324                     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);
0325                     //关键质量控制
0326                     //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
0327                     ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
0328                     ImageCodecInfo ici = null;
0329                     foreach (ImageCodecInfo i in icis)
0330                     {
0331                         if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
0332                         {
0333                             ici = i;
0334                         }
0335                     }
0336                     EncoderParameters ep = new EncoderParameters(1);
0337                     ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
0338                     //保存缩略图
0339                     templateImage.Save(fileSaveUrl, ici, ep);
0340                     //templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
0341                     //释放资源
0342                     templateG.Dispose();
0343                     templateImage.Dispose();
0344                     pickedG.Dispose();
0345                     pickedImage.Dispose();
0346                 }
0347             }
0348             //释放资源
0349             initImage.Dispose();
0350         }
0351         #endregion
0352         #region 等比缩放
0353         /// 
0354         /// 图片等比缩放
0355         /// 
0356         /// 
  
  
   
   吴剑 2011-01-21
  
  
0357         /// 原图HttpPostedFile对象
0358         /// 缩略图存放地址
0359         /// 指定的最大宽度
0360         /// 指定的最大高度
0361         /// 水印文字(为""表示不使用水印)
0362         /// 水印图片路径(为""表示不使用水印)
0363         public static void ZoomAuto(System.Web.HttpPostedFile postedFile, string savePath, System.Double targetWidth, System.Double targetHeight, string watermarkText, string watermarkImage)
0364         {
0365             //创建目录
0366             string dir = Path.GetDirectoryName(savePath);
0367             if (!Directory.Exists(dir))
0368                 Directory.CreateDirectory(dir);
0369             //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
0370             System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);
0371             //原图宽高均小于模版,不作处理,直接保存
0372             if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
0373             {
0374                 //文字水印
0375                 if (watermarkText != "")
0376                 {
0377                     using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(initImage))
0378                     {
0379                         System.Drawing.Font fontWater = new Font("黑体", 10);
0380                         System.Drawing.Brush brushWater = new SolidBrush(Color.White);
0381                         gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
0382                         gWater.Dispose();
0383                     }
0384                 }
0385                 //透明图片水印
0386                 if (watermarkImage != "")
0387                 {
0388                     if (File.Exists(watermarkImage))
0389                     {
0390                         //获取水印图片
0391                         using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
0392                         {
0393                             //水印绘制条件:原始图片宽高均大于或等于水印图片
0394                             if (initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height)
0395                             {
0396                                 Graphics gWater = Graphics.FromImage(initImage);
0397                                 //透明属性
0398                                 ImageAttributes imgAttributes = new ImageAttributes();
0399                                 ColorMap colorMap = new ColorMap();
0400                                 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
0401                                 colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
0402                                 ColorMap[] remapTable = { colorMap };
0403                                 imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
0404                                 float[][] colorMatrixElements = { 
0405                                         new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
0406                                         new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
0407                                         new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
0408                                         new float[] {0.0f,  0.0f,  0.0f,  0.5f, 0.0f},//透明度:0.5
0409                                         new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
0410                                         };
0411                                 ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
0412                                 imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
0413                                 gWater.DrawImage(wrImage, new Rectangle(initImage.Width - wrImage.Width, initImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);
0414                                 gWater.Dispose();
0415                             }
0416                             wrImage.Dispose();
0417                         }
0418                     }
0419                 }
0420                 //保存
0421                 initImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
0422             }
0423             else
0424             {
0425                 //缩略图宽、高计算
0426                 double newWidth = initImage.Width;
0427                 double newHeight = initImage.Height;
0428                 //宽大于高或宽等于高(横图或正方)
0429                 if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
0430                 {
0431                     //如果宽大于模版
0432                     if (initImage.Width > targetWidth)
0433                     {
0434                         //宽按模版,高按比例缩放
0435                         newWidth = targetWidth;
0436                         newHeight = initImage.Height * (targetWidth / initImage.Width);
0437                     }
0438                 }
0439                 //高大于宽(竖图)
0440                 else
0441                 {
0442                     //如果高大于模版
0443                     if (initImage.Height > targetHeight)
0444                     {
0445                         //高按模版,宽按比例缩放
0446                         newHeight = targetHeight;
0447                         newWidth = initImage.Width * (targetHeight / initImage.Height);
0448                     }
0449                 }
0450                 //生成新图
0451                 //新建一个bmp图片
0452                 System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
0453                 //新建一个画板
0454                 System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
0455                 //设置质量
0456                 newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
0457                 newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
0458                 //置背景色
0459                 newG.Clear(Color.White);
0460                 //画图
0461                 newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
0462                 //文字水印
0463                 if (watermarkText != "")
0464                 {
0465                     using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(newImage))
0466                     {
0467                         System.Drawing.Font fontWater = new Font("宋体", 10);
0468                         System.Drawing.Brush brushWater = new SolidBrush(Color.White);
0469                         gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
0470                         gWater.Dispose();
0471                     }
0472                 }
0473                 //透明图片水印
0474                 if (watermarkImage != "")
0475                 {
0476                     if (File.Exists(watermarkImage))
0477                     {
0478                         //获取水印图片
0479                         using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
0480                         {
0481                             //水印绘制条件:原始图片宽高均大于或等于水印图片
0482                             if (newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height)
0483                             {
0484                                 Graphics gWater = Graphics.FromImage(newImage);
0485                                 //透明属性
0486                                 ImageAttributes imgAttributes = new ImageAttributes();
0487                                 ColorMap colorMap = new ColorMap();
0488                                 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
0489                                 colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
0490                                 ColorMap[] remapTable = { colorMap };
0491                                 imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
0492                                 float[][] colorMatrixElements = { 
0493                                         new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},
0494                                         new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},
0495                                         new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},
0496                                         new float[] {0.0f,  0.0f,  0.0f,  0.5f, 0.0f},//透明度:0.5
0497                                         new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
0498                                         };
0499                                 ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
0500                                 imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
0501                                 gWater.DrawImage(wrImage, new Rectangle(newImage.Width - wrImage.Width, newImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);
0502                                 gWater.Dispose();
0503                             }
0504                             wrImage.Dispose();
0505                         }
0506                     }
0507                 }
0508                 //保存缩略图
0509                 newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
0510                 //释放资源
0511                 newG.Dispose();
0512                 newImage.Dispose();
0513                 initImage.Dispose();
0514             }
0515         }
0516         #endregion
0517         #region 其它
0518         /// 
0519         /// 判断文件类型是否为WEB格式图片
0520         /// (注:JPG,GIF,BMP,PNG)
0521         /// 
0522         /// HttpPostedFile.ContentType
0523         /// 
  
  
0524         public static bool IsWebImage(string contentType)
0525         {
0526             if (contentType == "image/pjpeg" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/bmp" || contentType == "image/png")
0527             {
0528                 return true;
0529             }
0530             else
0531             {
0532                 return false;
0533             }
0534         }
0535         #endregion
0536     }//end class
0537 }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值