/// <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; }
}
}