标签:style blog io ar color os sp for on
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MySystemManager.Utility 7 { 8 public class ImageTextAndPicter 9 { 10 /// <summary> 11 /// 生成缩略图 12 /// </summary> 13 /// <param name="originalImagePath">源图路径(物理路径)</param> 14 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 15 /// <param name="width">缩略图宽度</param> 16 /// <param name="height">缩略图高度</param> 17 /// <param name="mode">生成缩略图的方式</param> 18 public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 19 { 20 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 21 22 int towidth = width; 23 int toheight = height; 24 25 int x = 0; 26 int y = 0; 27 int ow = originalImage.Width; 28 int oh = originalImage.Height; 29 30 switch (mode) 31 { 32 case "HW"://指定高宽缩放(可能变形) 33 break; 34 case "W"://指定宽,高按比例 35 toheight = originalImage.Height * width / originalImage.Width; 36 break; 37 case "H"://指定高,宽按比例 38 towidth = originalImage.Width * height / originalImage.Height; 39 break; 40 case "Cut"://指定高宽裁减(不变形) 41 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 42 { 43 oh = originalImage.Height; 44 ow = originalImage.Height * towidth / toheight; 45 y = 0; 46 x = (originalImage.Width - ow) / 2; 47 } 48 else 49 { 50 ow = originalImage.Width; 51 oh = originalImage.Width * height / towidth; 52 x = 0; 53 y = (originalImage.Height - oh) / 2; 54 } 55 break; 56 default: 57 break; 58 } 59 60 //新建一个bmp图片 61 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 62 63 //新建一个画板 64 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 65 66 //设置高质量插值法 67 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 68 69 //设置高质量,低速度呈现平滑程度 70 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 71 72 //清空画布并以透明背景色填充 73 g.Clear(System.Drawing.Color.Transparent); 74 75 //在指定位置并且按指定大小绘制原图片的指定部分 76 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), 77 new System.Drawing.Rectangle(x, y, ow, oh), 78 System.Drawing.GraphicsUnit.Pixel); 79 80 try 81 { 82 //以jpg格式保存缩略图 83 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 84 } 85 catch (System.Exception e) 86 { 87 throw e; 88 } 89 finally 90 { 91 originalImage.Dispose(); 92 bitmap.Dispose(); 93 g.Dispose(); 94 } 95 } 96 /// <summary> 97 /// 在图片上增加文字水印 98 /// </summary> 99 /// <param name="Path">原服务器图片路径</param> 100 /// <param name="Path_sy">生成的带文字水印的图片路径</param> 101 /// <param name="addText">文字水印内容</param> 102 public void AddWater(string Path, string Path_sy, string addText) 103 { 104 105 System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 106 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); 107 g.DrawImage(image, 0, 0, image.Width, image.Height); 108 System.Drawing.Font f = new System.Drawing.Font("Verdana", 20);//字体大小 109 System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green); 110 //位置定位 111 g.DrawString(addText, f, b, 15, 15); 112 g.Dispose(); 113 114 image.Save(Path_sy); 115 image.Dispose(); 116 } 117 /// <summary> 118 /// 在图片上生成图片水印 119 /// </summary> 120 /// <param name="Path">原服务器图片路径</param> 121 /// <param name="Path_syp">生成的带图片水印的图片路径</param> 122 /// <param name="Path_sypf">水印图片路径</param> 123 public void AddWaterPic(string Path, string Path_syp, string Path_sypf) 124 { 125 System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 126 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); 127 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); 128 g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel); 129 g.Dispose(); 130 131 image.Save(Path_syp); 132 image.Dispose(); 133 } 134 135 } 136 }
标签:style blog io ar color os sp for on
原文地址:http://www.cnblogs.com/linsu/p/4138494.html