HTML部分
生成图片
function downloadCert() {
获取证书信息表选中行
var checkStatus = layuiTable.checkStatus('tabCertificate');
console.log(checkStatus);
判断当前是否只选择一条数据
if (checkStatus.data.length == 1) {
获取证书ID
var CertificateID = checkStatus.data[0].CertificateID;
window.open("DownloadCert?certificateId=" + CertificateID); }
else if (checkStatus.data.length > 1) {
layer.alert("只能选择一条数据生成图片!");}
else {
layer.alert("请选择一条数据生成图片!");}}
控制器部分
public ActionResult DownloadCert(int certificateId)
{ try {
查询证书信息
var dbCertificate = (from tbCertificate in myModels.CertificateITable
join tbAchievement in myModels.PW_Achievement on tbCertificate.AchievementID equals tbAchievement.AchievementID join tbUser in myModels.PW_User on tbAchievement.UserID equals tbUser.UserID
join tbStudent in myModels.PW_Student on tbUser.UserID equals tbStudent.UserID
where tbCertificate.CertificateID == certificateId
select new
{ tbStudent.StudentName,考生姓名
tbAchievement.StTime,考试时间
tbAchievement.Achievement,成绩
tbCertificate.CertificateNumber,证书编号
tbCertificate.IssuanceTime证书颁发时间
}).Single();
获取空白证书的物理地址
string filePath = Server.MapPath("~/Content/images/Credential2.png");
将图片转化为位图(栅格图)
Bitmap bitmap = new Bitmap(filePath);
封装一个绘画图面
Graphics g = Graphics.FromImage(bitmap);
定义单色的画笔
SolidBrush solidBrush = new SolidBrush(Color.Black);
DrawString:在指定的位置用指定的画笔(Brush)和字体(Font)对象绘制指定的文本字符串
DrawString(要绘制的文本内容,所使用的字体,绘制文本的画刷,开始绘制的坐标点)
考生姓名
g.DrawString(dbCertificate.StudentName, new Font("宋体", 14), solidBrush, 110, 146);
时间
.DrawString(dbCertificate.StTime.Value.Year.ToString(), new Font("宋体", 14), solidBrush, 105, 175);
g.DrawString(dbCertificate.StTime.Value.Month.ToString(), new Font("宋体", 14), solidBrush, 173, 175);
g.DrawString(dbCertificate.StTime.Value.Day.ToString(), new Font("宋体", 14), solidBrush, 220, 175);
绘制成绩
g.DrawString(dbCertificate.Achievement.ToString(), new Font("宋体", 14), solidBrush, 184, 200);
绘制证书编号
g.DrawString(dbCertificate.CertificateNumber.Trim(), new Font("宋体", 14), solidBrush, 150, 268);
绘制证书时间
string strDate = dbCertificate.IssuanceTime.Value.ToString("yyyy年MM月dd日");
g.DrawString(strDate, new Font("宋体", 14), solidBrush, 340, 332);
MemoryStream ms = new MemoryStream();
将图像以jpeg的格式保存到流中
bitmap.Save(ms, ImageFormat.Jpeg);
以byte类型输出
byte[] newByte = ms.ToArray();
return File(newByte, @"image/jpeg");}
catch (Exception e)
{ Console.WriteLine(e);
return Json("数据异常", JsonRequestBehavior.AllowGet);}}
关于图片的生成,首先要给那些考试合格的同学生成证书,生成证书之后,再打那里面选中要生成图片的数据,点击生成图片按钮,他就会在另一个页面生成你想要生成的图片,且这个功能只能一次生成一个图片。截图如下: