
在开发中有一些场景中我们需要将文档盖章、荣誉证书生成等,此篇记录在.Net Mvc 中合成图片
以下是本篇文章正文内容,下面案例可供参考
一、封装ImgText对象命名空间:
using System; using System.Collections.Generic; using System.Drawing; using System.Text;
ImgText对象:
public class ImgText
{
///
/// 图片文字
///
/// 文字
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = Color.Black;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font("宋体", 28, FontStyle.Regular, GraphicsUnit.Point);
this.PointF = new PointF(x, y-(28/2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = Color.Black;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font("宋体", fontSize, FontStyle.Regular, GraphicsUnit.Point);
this.PointF = new PointF(x, y-(fontSize/2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 字体样式 例如:加粗
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, FontStyle fontStyle, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = Color.Black;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font("宋体", fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y-(fontSize/2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 字体颜色
/// 字体样式
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, Color color, FontStyle fontStyle, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = color;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font("宋体", fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y - (fontSize / 2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 字体颜色
/// 字体样式
/// 字体类型 例如:微软雅黑、宋体
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, Color color, FontStyle fontStyle, FontFamily fontFamily, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = color;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y - (fontSize / 2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 字体颜色
/// 字体样式
/// 字体类型
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, Color color, FontStyle fontStyle, string fontFamily, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = color;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y - (fontSize / 2));
}
///
/// 图片文字
///
/// 文字
/// /// 字体大小
/// 对齐方式
/// 字体颜色
/// 字体样式
/// 字体类型
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize,StringAlignment alignment, Color color, FontStyle fontStyle, string fontFamily, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = alignment;
//默认设置为靠左布局
this.StringFormat = sf;
//默认画笔颜色为黑色
this.Color = color;
//默认字体为是宋体 字体大小为28 普通文本
this.Font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y - (fontSize / 2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 对齐方式
/// 字体颜色
/// 字体
/// 字体样式
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, StringAlignment alignment, Color color, FontFamily fontFamily, FontStyle fontStyle, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = alignment;
this.StringFormat = sf;
this.Color = color;
this.Font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y - (fontSize / 2));
}
///
/// 图片文字
///
/// 文字
/// 字体大小
/// 对齐方式
/// 字体颜色
/// 字体样式
/// 字体类型
/// 要插入的x轴位置,根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的x轴位置,Center则以文字中心点为插入点的x轴位置
/// 要插入的y轴 根据StringAlignment类型不同插入点位置不同,例如Near则以左边为插入点的y轴位置,Center则以文字中心点为插入点的y轴位置
public ImgText(string text, float fontSize, StringAlignment alignment, Color color, string fontFamily, FontStyle fontStyle, float x, float y)
{
this.Text = text;
StringFormat sf = new StringFormat();
sf.Alignment = alignment;
this.StringFormat = sf;
this.Color = color;
this.Font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Point);
this.PointF = new PointF(x, y - (fontSize / 2));
}
///
/// 要插入的文字
///
public string Text { get; set; }
///
/// 文字布局方式
///
public StringFormat StringFormat { get; set; }
///
/// 字体颜色
///
public Color Color { get; set; }
///
/// 字体样式大小等
///
public Font Font { get; set; }
///
/// 字体要插入的位置
///
public PointF PointF { get; set; }
}
二、ImageUtil帮助类
命名空间:
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; using System.IO; using System.Text; using System.Web; using System.Web.Mvc;
ImageUtil帮助类:
public static class ImageUtil
{
public static Bitmap AsBitmap(this Image sourceImage)
{
Bitmap bitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(sourceImage, 0, 0);
}
return bitmap;
}
///
/// 图片合并 例如,图片盖章
///
/// 底图、背景图
/// 最上层图片、盖章图片
/// x轴位置 图片左上角的点
/// y轴位置 图片左上角的点
///
public static Bitmap SyntheticImage(this Image backgroundImg, Image topImg, float xDeviation = 0, float yDeviation = 0)
{
Bitmap bmp = new Bitmap(backgroundImg.Width, backgroundImg.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawImage(backgroundImg, 0, 0, backgroundImg.Width, backgroundImg.Height);
g.DrawImage(topImg, xDeviation, yDeviation, topImg.Width, topImg.Height);
}
return bmp;
}
///
/// 图片合并 例如,图片盖章
///
/// 底图、背景图
/// 最上层图片、盖章图片
/// x轴位置 图片左上角的点
/// y轴位置 图片左上角的点
///
public static Bitmap SyntheticImage(this Bitmap backgroundImg, Image topImg, float xDeviation = 0, float yDeviation = 0)
{
Bitmap bmp = new Bitmap(backgroundImg.Width, backgroundImg.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawImage(backgroundImg, 0, 0, backgroundImg.Width, backgroundImg.Height);
g.DrawImage(topImg, xDeviation, yDeviation, topImg.Width, topImg.Height);
}
return bmp;
}
///
/// 图片合并 例如,图片盖章
///
/// 底图、背景图
/// 最上层图片、盖章图片
/// x轴位置 图片左上角的点
/// y轴位置 图片左上角的点
/// 插入宽度
/// 插入高度
///
public static Bitmap SyntheticImage(this Image backgroundImg, Image topImg, float xDeviation = 0, float yDeviation = 0, float width = 0, float height = 0)
{
Bitmap bmp = new Bitmap(backgroundImg.Width, backgroundImg.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawImage(backgroundImg, 0, 0, backgroundImg.Width, backgroundImg.Height);
g.DrawImage(topImg, xDeviation, yDeviation, width != 0 ? width : topImg.Width, height != 0 ? height : topImg.Height);
}
return bmp;
}
///
/// 图片合并 例如,图片盖章
///
/// 底图、背景图
/// 最上层图片、盖章图片
/// x轴位置 图片左上角的点
/// y轴位置 图片左上角的点
/// 插入宽度
/// 插入高度
///
public static Bitmap SyntheticImage(this Bitmap backgroundImg, Image topImg, float xDeviation = 0, float yDeviation = 0, float width = 0, float height = 0)
{
Bitmap bmp = new Bitmap(backgroundImg.Width, backgroundImg.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawImage(backgroundImg, 0, 0, backgroundImg.Width, backgroundImg.Height);
g.DrawImage(topImg, xDeviation, yDeviation, width != 0 ? width : topImg.Width, height != 0 ? height : topImg.Height);
}
return bmp;
}
///
/// 插入文字
///
///
///
///
public static Bitmap InsertString(ImgText imgText, Bitmap sourceImage)
{
Bitmap bitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(sourceImage, 0, 0);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
using (SolidBrush brush = new SolidBrush(imgText.Color))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawString(imgText.Text, imgText.Font, brush, imgText.PointF, imgText.StringFormat);
}
}
return bitmap;
}
///
/// 插入文字
///
///
///
///
public static Bitmap InsertString(ImgText imgText, Image sourceImage)
{
Bitmap bitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(sourceImage, 0, 0);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
using (SolidBrush brush = new SolidBrush(imgText.Color))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawString(imgText.Text, imgText.Font, brush, imgText.PointF, imgText.StringFormat);
}
}
return bitmap;
}
///
/// 插入文字
///
///
///
///
public static Bitmap InsertString(this Image sourceImage, ImgText imgText)
{
Bitmap bitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(sourceImage, 0, 0);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
using (SolidBrush brush = new SolidBrush(imgText.Color))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawString(imgText.Text, imgText.Font, brush, imgText.PointF, imgText.StringFormat);
}
}
return bitmap;
}
///
/// 插入文字
///
///
///
///
public static Bitmap InsertString(this Bitmap sourceImage, ImgText imgText)
{
Bitmap bitmap = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(sourceImage, 0, 0);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
using (SolidBrush brush = new SolidBrush(imgText.Color))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawString(imgText.Text, imgText.Font, brush, imgText.PointF, imgText.StringFormat);
}
}
return bitmap;
}
///
/// 将位图转换为Base64字符串 在前端可用
///
///
///
public static string ToBase64String(this Bitmap bitmap)
{
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
}
///
/// 将位图转换为FileStreamResult 在前端可用
///
///
///
public static ActionResult ToFile(this Bitmap bitmap,string fileName)
{
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return new FileStreamResult(new MemoryStream(arr), "image/jpeg") { FileDownloadName = JoinExt(fileName, ".jpg") };
}
}
///
/// 如果文件名中没有后缀名,增加文件后缀名
///
///
/// 文件后缀名 例如 .jpg .zip .pdf
///
private static string JoinExt(string fileName,string ext)
{
if (!fileName.EndsWith(ext))
{
fileName += ext;
}
return HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
}
}
三、测试
控制器中编写:
////// 测试图片合成 /// ///[HttpGet] public ActionResult GetSyntheticImg(string id) { string _filerootdir = _webhostEnvironment.WebRootPath; //可以通过id去查询相关数据,这里只做测试数据 //模板图片 var imgPath = _filerootdir+ "/img/test_bj.jpg"; return Image.FromFile(imgPath).AsBitmap().InsertString(new ImgText("Hello Word", 20,StringAlignment.Center, Color.Black, FontStyle.Regular, "微软雅黑", 300,210 )).ToFile("测试图片"); }
前台编写:
Base64String方式(可在控制器post方法使用):
Bitmap bitmap = Image.FromFile(Path.Combine(HttpContext.Current.Server.MapPath("~/administrator/images/certificate/2.jpg")))
.SyntheticImage(Image.FromFile(Path.Combine(HttpContext.Current.Server.MapPath("~/administrator/images/certificate/yz.png"))), 1360, 1100, 280, 280)
.InsertString(new ImgText("XXXXXXXXXXXXX分公司", 23, Color.Black, FontStyle.Regular, "微软雅黑", 340, 1045))
.InsertString(new ImgText("张三", 23, Color.Black, FontStyle.Regular, "微软雅黑", 1490, 1045))
.InsertString(new ImgText("XXXXXXXXX大厦", 23, Color.Black, FontStyle.Regular, "微软雅黑", 340, 1100 ))
.InsertString(new ImgText("XXX-D342", 23, Color.Black, FontStyle.Regular, "微软雅黑", 1490, 1100))
.InsertString(new ImgText(DateTime.Now.ToString("yyyy-MM-dd"), 23, Color.Black, FontStyle.Regular, "微软雅黑", 340, 1155))
.InsertString(new ImgText("XXXXXX机构", 23, Color.Black, FontStyle.Regular, "微软雅黑", 1490, 1155 ))
.InsertString(new ImgText("XXXXXX荣誉",55, StringAlignment.Center, Color.Black, FontStyle.Regular, "微软雅黑", 980, 690 ));
String imgtest64str = bitmap?.ToBase64String();
前端使用:
以上就是在.Net Mvc 下图片合成的简单使用。