给图片批量添加水印
效果与商城上的效果类似
protected void Add_Click(object sender, EventArgs e) { DirectoryInfo theFolder = new DirectoryInfo(Request.PhysicalApplicationPath + "Images\\Product"); //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { foreach (FileInfo NextFile in NextFolder.GetFiles()) { if (NextFile.Name.Contains("SY800SY")) { File.Delete(NextFile.FullName); } else if (!NextFile.Name.Contains("SY.") && !NextFile.Name.Contains("SY800.")) { if (!NextFile.Name.Contains("47-47_") && !NextFile.Name.Contains("50-50_") && !NextFile.Name.Contains("80-80_") && !NextFile.Name.Contains("100-100_") && !NextFile.Name.Contains("120-120_") && !NextFile.Name.Contains("140-140_") && !NextFile.Name.Contains("160-160_") && !NextFile.Name.Contains("380-380_")) { MarkWater(NextFile.FullName, Request.PhysicalApplicationPath + "images\\" + "logo-back.png"); } } } } }
给图片添加水印的方法
/// <summary> /// 给图片添加水印 /// </summary> /// <param name="filePath">原图片地址</param> /// <param name="waterFile">水印图片地址</param> private void MarkWater(string filePath, string waterFile) { int i = filePath.LastIndexOf("."); string ex = filePath.Substring(i, filePath.Length - i); if (string.Compare(ex, ".gif", true) == 0) { return; } int newp = filePath.LastIndexOf("."); string newpo = filePath.Substring(0, newp); string newpolast = filePath.Substring(newp + 1); string newlastimg = newpo + "SY800" + "." + newpolast; BitmapHelper.MakeThumbnail(filePath, newlastimg, 800, 800, "DB", "JPG"); string ModifyImagePath = newlastimg; int lucencyPercent = 25; System.Drawing.Image modifyImage = null; System.Drawing.Image drawedImage = null; Graphics g = null; try { modifyImage = System.Drawing.Image.FromFile(ModifyImagePath, true); drawedImage = System.Drawing.Image.FromFile(waterFile, true); g = Graphics.FromImage(modifyImage); int x = (modifyImage.Width - drawedImage.Width) / 2; int y = (modifyImage.Height - drawedImage.Height) / 2; float[][] matrixItems =//控制水印图片的透明度 { new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, (float) lucencyPercent/100f, 0}, new float[] {0, 0, 0, 0, 1} }; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imgAttr = new ImageAttributes(); imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage(drawedImage, new Rectangle(x, y, drawedImage.Width, drawedImage.Height), 10, 10, drawedImage.Width, drawedImage.Height, GraphicsUnit.Pixel, imgAttr); string[] allowImageType = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" }; FileInfo fi = new FileInfo(ModifyImagePath); ImageFormat imageType = ImageFormat.Gif; switch (fi.Extension.ToLower()) { case ".jpg": imageType = ImageFormat.Jpeg; break; case ".gif": imageType = ImageFormat.Gif; break; case ".png": imageType = ImageFormat.Png; break; case ".bmp": imageType = ImageFormat.Bmp; break; case ".tif": imageType = ImageFormat.Tiff; break; case ".wmf": imageType = ImageFormat.Wmf; break; case ".ico": imageType = ImageFormat.Icon; break; default: break; } MemoryStream ms = new MemoryStream(); modifyImage.Save(ms, imageType); byte[] imgData = ms.ToArray(); modifyImage.Dispose(); drawedImage.Dispose(); g.Dispose(); FileStream fs = null; fs = new FileStream(ModifyImagePath.Replace("SY800.", "SY."), FileMode.Create, FileAccess.Write); if (fs != null) { fs.Write(imgData, 0, imgData.Length); fs.Close(); } } finally { try { drawedImage.Dispose(); modifyImage.Dispose(); g.Dispose(); } catch { } } }
用到的一个生成缩略图的类
public class BitmapHelper { /// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> /// <param name="type">缩略图的图片类型</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string type) { Image originalImage = Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case "HW"://指定高宽缩放(可能变形) break; case "W"://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case "H"://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case "Cut"://指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放) if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight) { toheight = height; towidth = originalImage.Width * height / originalImage.Height; } else { towidth = width; toheight = originalImage.Height * width / originalImage.Width; } break; default: break; } //新建一个bmp图片 Image bitmap = new Bitmap(towidth, toheight); //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //保存缩略图 if (type.ToUpper() == "JPG") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } if (type.ToUpper() == "BMP") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); } if (type.ToUpper() == "GIF") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); } if (type.ToUpper() == "PNG") { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } }
基本上就Ok 了 自己记录的文章没有仔细排版 也没有多少文字说明