找了半天废了点功夫,记录一下,同时也为后来的兄弟指引一下,这种现成的东西别浪费时间了。
图片加水印
using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Drawing; using System.Text; namespace Yadinghao.Framework.Utils { public class ImageWaterMarkUtils { /// <summary> /// 添加水印 /// </summary> /// <param name="imgPath">原图片地址</param> /// <param name="sImgPath">加水印厚图片地址</param> /// <param name="text">文字</param> /// <param name="size">大小-30</param> /// <param name="location">012左中右</param> /// <param name="c">颜色</param> /// <returns>成功,失败 </returns> public static bool AddWaterMarkText(string imgPath, string sImgPath, string text, int size, int location, Color c) { using (Image image = Image.FromFile(imgPath)) { try { Bitmap bitmap = new Bitmap(image); int width = bitmap.Width, height = bitmap.Height; Graphics g = Graphics.FromImage(bitmap); g.DrawImage(bitmap, 0, 0); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel); Font crFont = new Font("微软雅黑", size, FontStyle.Bold); SizeF crSize = new SizeF(); crSize = g.MeasureString(text, crFont);//文字大小 //背景位置(去掉了. 如果想用可以自己调一调 位置.) //graphics.FillRectangle(new SolidBrush(Color.FromArgb(200, 255, 255, 255)), (width - crSize.Width) / 2, (height - crSize.Height) / 2, crSize.Width, crSize.Height); SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(120, c.R, c.G, c.B)); //将原点移动 到图片中点 if (location == 0) { g.TranslateTransform(10, 10); } else if (location == 1) { g.TranslateTransform((width - crSize.Width) / 2, 10); } else { g.TranslateTransform(width - crSize.Width - 10, 10); } g.DrawString(text, crFont, semiTransBrush, new PointF(0, 0)); //保存文件 bitmap.Save(sImgPath, ImageFormat.Jpeg); return true; } catch (Exception e) { return false; } } } } }
调用
ImageWaterMarkUtils.AddWaterMarkText("D:\Yadinghao\Image\js.jpg", "D:\Yadinghao\Image\TT\js.jpg","亚丁号",18,2,Color.Red);
生成图片缩略图:
public static void ImageThumb(string originalImagePath, string thumbnailPath, int width, int height, ThumbnailMode mode) { System.Drawing.Image img_OriginalImage = null; ImageFormat tFormat = null; try { img_OriginalImage = (Bitmap)System.Drawing.Image.FromFile(originalImagePath); tFormat = img_OriginalImage.RawFormat; } catch (Exception ex) { if (img_OriginalImage != null) { img_OriginalImage.Dispose(); } throw new Exception("压缩图片:" originalImagePath "失败! " ex.Message); } int i_ToWidth = width; int i_ToHeight = height; int x = 0; int y = 0; int i_OriginalWidth = img_OriginalImage.Width; int i_OriginalHeight = img_OriginalImage.Height; switch (mode) { case ThumbnailMode.Width: i_ToHeight = img_OriginalImage.Height * width / img_OriginalImage.Width; break; case ThumbnailMode.Height: i_ToWidth = img_OriginalImage.Width * height / img_OriginalImage.Height; break; case ThumbnailMode.Cut: if ((double)img_OriginalImage.Width / (double)img_OriginalImage.Height > (double)i_ToWidth / (double)i_ToHeight) { i_OriginalHeight = img_OriginalImage.Height; i_OriginalWidth = img_OriginalImage.Height * i_ToWidth / i_ToHeight; y = 0; x = (img_OriginalImage.Width - i_OriginalWidth) / 2; } else { i_OriginalWidth = img_OriginalImage.Width; i_OriginalHeight = img_OriginalImage.Width * height / i_ToWidth; x = 0; y = (img_OriginalImage.Height - i_OriginalHeight) / 2; } break; default: break; } //新建一个BMP图片 System.Drawing.Image img_BitMap = new System.Drawing.Bitmap(i_ToWidth, i_ToHeight); //新建一个画板 System.Drawing.Graphics gp = Graphics.FromImage(img_BitMap); //设置高质量插值法 gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量、低速度呈现平滑程度 gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gp.CompositingQuality = CompositingQuality.HighQuality; //清空画布并以透明背景色填充 gp.Clear(Color.Transparent); //指定位置并按大小绘制原图片的指定部分 gp.DrawImage(img_OriginalImage, new Rectangle(0, 0, i_ToWidth, i_ToHeight), new Rectangle(x, y, i_OriginalWidth, i_OriginalHeight), GraphicsUnit.Pixel); try { EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = 100;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int i = 0; i < arrayICI.Length; i ) { if (arrayICI[i].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[i]; break; } } if (jpegICIinfo != null && Equals(tFormat, ImageFormat.Jpeg)) { //以JPG格式保存图片 img_BitMap.Save(thumbnailPath, jpegICIinfo, ep); //Const.iSuccessCount ; } else { img_BitMap.Save(thumbnailPath, tFormat); //Const.iSuccessCount ; } } catch (Exception ex) { Console.WriteLine("复制图片:" originalImagePath "失败! " ex.Message ex.StackTrace); } finally { if (img_OriginalImage != null) { img_OriginalImage.Dispose(); } if (img_BitMap != null) { img_BitMap.Dispose(); } if (gp != null) { gp.Dispose(); } //GC.Collect(); } } public enum ThumbnailMode { /// <summary> /// 指定宽度,高度按照比例缩放 /// </summary> Width = 0, /// <summary> /// 指定高度,宽度按照比例缩放 /// </summary> Height = 1, /// <summary> /// 按照指定的高度和宽度剪裁 /// </summary> Cut = 2, /// <summary> /// 指定宽度,高度按照比例缩放 /// </summary> None = 3 }
调用:
ImageThumbUtils.ImageThumb("D:\Yadinghao\Image\apk.jpg", "D:\Yadinghao\Image\TT\apk.jpg", 220, 150, ImageThumbUtils.ThumbnailMode.Cut);
好了大家直接使用就可以了。