C#简单制作封面图
Posted 互联网功能小例子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#简单制作封面图相关的知识,希望对你有一定的参考价值。
封面图简单设计-1
封面图简答设计-2
var data = {
backgroundWidth: 800, //底图宽度
backgroundHeight: 800, //底图高度
backgroundColor: "#099dff", //底图背景颜色
"text.content": 'C#创建多张|单色图组合新图', //文字
"text.color": '#fff', //文字颜色
"text.x": 0, //文字在底图的x轴位置,默认自动计算水平和垂直居中
"text.y": 0, //文字在底图的y轴位置
"text.fontSize": 80, //文字大小
"text.fontFamily": '鸿雷板书简体', //文字字体
"lineList[0].lineRotate": 0, //第一张装饰图的旋转角度
"lineList[0].lineWidth": 150, //第一张装饰图的宽度
"lineList[0].lineHeight": 1200, //第一张装饰图的高度
"lineList[0].lineColor": "#0aadff", //第一张装饰图的颜色
"lineList[0].lineInBackgroundXValue": 350, //第一张装饰图的在底图的x轴位置
"lineList[0].lineInBackgroundYValue": -300, //第一张装饰图的在底图的y轴位置
"lineList[1].lineRotate": 0,
"lineList[1].lineWidth": 150,
"lineList[1].lineHeight": 1200,
"lineList[1].lineColor": "#0bbdff",
"lineList[1].lineInBackgroundXValue": 500,
"lineList[1].lineInBackgroundYValue": -300,
"lineList[2].lineRotate": 0,
"lineList[2].lineWidth": 150,
"lineList[2].lineHeight": 1200,
"lineList[2].lineColor": "#0ccdff",
"lineList[2].lineInBackgroundXValue": 650,
"lineList[2].lineInBackgroundYValue": -300
};
#region 背景图制作 - 综合应用 - 动态化参数
public class SetImageBackgroundModel
{
public int backgroundWidth { get; set; }
public int backgroundHeight { get; set; }
public string backgroundColor { get; set; }
public SetImageBackgroundTextModel text { get; set; }
public List<SetImageBackgroundLineModel> lineList { get; set; }
}
public class SetImageBackgroundLineModel
{
public int lineRotate { get; set; }
public int lineWidth { get; set; }
public int lineHeight { get; set; }
public string lineColor { get; set; }
public int lineInBackgroundXValue { get; set; }
public int lineInBackgroundYValue { get; set; }
}
public class SetImageBackgroundTextModel
{
public string content { get; set; }
public int x { get; set; }
public int y { get; set; }
public string color { get; set; }
public int fontSize { get; set; }
public string fontFamily { get; set; }
}
public ActionResult SetImageBackground(SetImageBackgroundModel model)
{
try
{
if (model.backgroundWidth <= 0|| model.backgroundHeight <= 0)
{
return Json(new { });
}
if (string.IsNullOrEmpty(model.backgroundColor))
{
return Json(new { });
}
//微信公众号封面大小:476x200
//创建一张1904x800
//背景图
Bitmap bitmapBackground = new Bitmap(model.backgroundWidth, model.backgroundHeight);
using (Graphics graphics = Graphics.FromImage(bitmapBackground))
{
Color color = ColorTranslator.Fromhtml(model.backgroundColor);
graphics.FillRectangle(new SolidBrush(color), new Rectangle(0, 0, model.backgroundWidth, model.backgroundHeight));
}
//装饰图 - 循环
foreach(SetImageBackgroundLineModel item in model.lineList)
{
//创建图片
Bitmap bitmap = new Bitmap(item.lineWidth, item.lineHeight);
using (Graphics g = Graphics.FromImage(bitmap))
{
Color color = ColorTranslator.FromHtml(item.lineColor);
g.FillRectangle(new SolidBrush(color), new Rectangle(0, 0, item.lineWidth, item.lineHeight));
}
//图片叠加在背景图上 - 同时旋转一定的角度
using (Graphics g = Graphics.FromImage(bitmapBackground))
{
g.RotateTransform(item.lineRotate); //旋转度数 - 在父级上进行旋转
Rectangle _r = new Rectangle(item.lineInBackgroundXValue, item.lineInBackgroundYValue, bitmap.Width, bitmap.Height);
int bitmap_x = 0;
int bitmap_y = 0;
g.DrawImage(bitmap, _r, bitmap_x, bitmap_y, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
g.ResetTransform(); //重置
}
}
//叠加文字
using (Graphics g = Graphics.FromImage(bitmapBackground))
{
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
using (Font f = new Font(model.text.fontFamily, model.text.fontSize))
{
using (Brush b = new SolidBrush(Color.White))
{
SizeF size = g.MeasureString(model.text.content, new Font(model.text.fontFamily, model.text.fontSize, System.Drawing.FontStyle.Regular));
float x = (bitmapBackground.Width - size.Width) / 2; //水平居中
float y = (bitmapBackground.Height - size.Height) / 2; //垂直居中
g.DrawString(model.text.content, f, b, x, y);
}
}
}
//生成图片
string rootPath = Server.MapPath("~");
string folderPath = "/NewTextImage/";
string timePath = DateTime.Now.ToString("yyyy-MM-dd") + "/";
if (Directory.Exists(rootPath + folderPath + timePath) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(rootPath + folderPath + timePath);
}
bitmapBackground.Save(rootPath + folderPath + timePath + $"SetImageBackground_{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg");
}
catch (Exception ex)
{
}
return Json(new { });
}
#endregion
聊一聊你想实现的小功能或者遇到的问题,留言交给你
Send Info To Me,Thanks!
以上是关于C#简单制作封面图的主要内容,如果未能解决你的问题,请参考以下文章