生成图片

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

 

关于图片的生成,首先要给那些考试合格的同学生成证书,生成证书之后,再打那里面选中要生成图片的数据,点击生成图片按钮,他就会在另一个页面生成你想要生成的图片,且这个功能只能一次生成一个图片。截图如下:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值