1. 程式人生 > 其它 >圖片新增文字或者圖片水印

圖片新增文字或者圖片水印

  /// <summary>
        /// 生成模版
        /// </summary>
        /// <param name="backgroundpath">背景圖片地址</param>
        /// <param name="saveFile">生成模版圖片地址</param>
        /// <param name="width">模版寬頻</param>
        /// <param name="height">模版高度</param>
        /// <param name="waterInfos">
水印資訊</param> /// <returns></returns> public static bool CreatePicTemple(string backgroundpath, ref string saveFile, List<WaterInfo> waterInfos = null, int width = 0, int height = 0) { try { //1.讀取背景圖片 //2.縮放高寬
//3.讀取水印圖片 或者文字 //4.畫圖 string filebackgroundpath = System.Web.HttpContext.Current.Server.MapPath(backgroundpath); Image imgagephoto = Image.FromFile(filebackgroundpath); if (width != 0 && height != 0 && (imgagephoto.Width != width || imgagephoto.Height != height)) {
var fi = new FileInfo(filebackgroundpath); string thumbnailname = fi.Name.Split('.')[0] + "_thumbnail" + fi.Extension; string thumbnaipath = fi.DirectoryName + "\\" + thumbnailname; UploadCommon.MakeThumbnail(filebackgroundpath, thumbnaipath, width, height, "A"); filebackgroundpath = thumbnaipath; } imgagephoto = Image.FromFile(filebackgroundpath); if (waterInfos != null && waterInfos.Count > 0) { Graphics g = Graphics.FromImage(imgagephoto); //建立畫布 waterInfos.ForEach(c => { switch (c.WaterType) { case EnumWaterType.Word: FontTextStyle fontTextStyle; if (!string.IsNullOrWhiteSpace(c.IFoinTextStyle)) { fontTextStyle = Newtonsoft.Json.JsonConvert.DeserializeObject<FontTextStyle>(c.IFoinTextStyle); if (fontTextStyle != null) { fontTextStyle.FamilyName = fontTextStyle.FamilyName ?? "微軟雅黑"; fontTextStyle.FontSize = fontTextStyle.FontSize ?? 12; fontTextStyle.iFontStyle = fontTextStyle.iFontStyle ?? FontStyle.Regular; fontTextStyle.iColor = fontTextStyle.iColor ?? Color.Black; } } else { fontTextStyle = new FontTextStyle() { FamilyName = "微軟雅黑", FontSize = 12, iFontStyle = FontStyle.Regular, iColor = Color.Black }; } var drawFont = new Font(fontTextStyle.FamilyName, (int)fontTextStyle.FontSize, (FontStyle)fontTextStyle.iFontStyle); g.DrawString(c.WaterMarkText, drawFont, new SolidBrush((Color)fontTextStyle.iColor), c.WaterPoint); break; case EnumWaterType.Pic: Image waterimage = Image.FromFile(c.PicPath); PicStyle picStyle; if (!string.IsNullOrWhiteSpace(c.IPicStyle)) { //設定圖片樣式 picStyle = Newtonsoft.Json.JsonConvert.DeserializeObject<PicStyle>(c.IPicStyle); if (picStyle != null) { picStyle.Width = picStyle.Width ?? waterimage.Width; picStyle.Height = picStyle.Height ?? waterimage.Height; } else { picStyle = new PicStyle { Width = waterimage.Width, Height = waterimage.Height }; } } else { picStyle = new PicStyle { Width = waterimage.Width, Height = waterimage.Height }; } Image imgWatermark = new Bitmap(waterimage, (int)picStyle.Width, (int)picStyle.Height); g.DrawImage(imgWatermark, c.WaterPoint); break; } }); g.Dispose(); string saveFilePath = System.Web.HttpContext.Current.Server.MapPath(saveFile); var savePathDir = saveFilePath.Replace(Path.GetFileName(saveFilePath), ""); //要儲存的圖片二維碼目錄 //判斷目錄 不存在 建立 if (!string.IsNullOrWhiteSpace(savePathDir)) { try { if (!Directory.Exists(savePathDir)) { Directory.CreateDirectory(savePathDir); } } catch (Exception) { throw; } imgagephoto.Save(saveFilePath); imgagephoto.Dispose(); } } return true; } catch (Exception) { return false; } } /// <summary> /// 水印資訊 /// </summary> public class WaterInfo { //水印型別 public EnumWaterType WaterType { get; set; } /// <summary> /// 水印座標 /// </summary> public PointF WaterPoint { get; set; } /// <summary> /// 文字水印 內容 /// </summary> public string WaterMarkText { get; set; } /// <summary> /// 文字水印樣式 /// </summary> public string IFoinTextStyle { get; set; } /// <summary> /// 圖片水印地址 /// </summary> public string PicPath { get; set; } /// <summary> /// 圖片使用樣式 /// </summary> public string IPicStyle { get; set; } } // 水印型別 Word-文字 Pic-圖片 public enum EnumWaterType { Word, Pic } /// <summary> /// 字型樣式設定 /// </summary> private class FontTextStyle { //字型 public string FamilyName { get; set; } //文字大小 public int? FontSize { get; set; } //字型樣式 public FontStyle? iFontStyle { get; set; } //文字顏色 public Color? iColor { get; set; } } /// <summary> /// 圖片屬性 /// </summary> private class PicStyle { //圖片寬度 public int? Width { get; set; } //圖片高度 public int? Height { get; set; } } }