GDI+生成证书带二维码

     最近做一个在线培训系统,要求学生考试成绩合格后自动生成证书,并提供下载。证书上的二维码使用ThoughtWorks.QRCode生成,支持中文(使用快拍
二维码测试正常)。
                #region 绘制基本数据
                var certificateBackgroundImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Images/3.jpg"));
                Graphics gaphics = Graphics.FromImage(certificateBackgroundImage);
                //使用已有的照片作为背景
                TextureBrush br = new TextureBrush(certificateBackgroundImage);
                Rectangle rc = new Rectangle(0, 0, 1000, 750);
                gaphics.FillRectangle(br, rc);
                Font ft = new Font("微软雅黑", 17);
                //在指定的位置绘制文本
                gaphics.DrawString("" + Model.TrueName + "", ft, Brushes.Black, 115, 250);
                gaphics.DrawString("" + Model.IdentityCardNumber + "", ft, Brushes.Black, 460, 250);
                gaphics.DrawString("" + professionName + "", ft, Brushes.Black, 115, 295);
                gaphics.DrawString("" + Model.WorkUnit + "", ft, Brushes.Black, 460, 295);
                gaphics.DrawString("语文,数学", ft, Brushes.Black, 200, 345);
                gaphics.DrawString("50", ft, Brushes.Black, 673, 345);
                gaphics.DrawString("物理,化学", ft, Brushes.Black, 200, 442);
                gaphics.DrawString("50", ft, Brushes.Black, 673, 442);
                gaphics.DrawString("" + DateTime.Now.Year + "", ft, Brushes.Black, 750, 675);
                gaphics.DrawString("" + DateTime.Now.Month + "", ft, Brushes.Black, 825, 675);
                gaphics.DrawString("" + DateTime.Now.Day + "", ft, Brushes.Black, 875, 675);
                //绘制照片
                using (System.Drawing.Image image = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Images/boy.jpg")))
                {
                    gaphics.DrawImage(image, new Rectangle(820, 245, 130, 182));
                }
                //生成证书编码
                gaphics.DrawString("" + CertificateNumber + "", ft, Brushes.Black, 820, 435);

                #endregion
View Code
 
                #region 生成二维码
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                string encoding = "Byte";
                switch (encoding)
                {
                    case "Byte":
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                        break;
                    case "AlphaNumeric":
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
                        break;
                    case "Numeric":
                        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
                        break;
                }
                int scale = 4;
                qrCodeEncoder.QRCodeScale = scale;
                string errorCorrect = "L";
                switch (errorCorrect)
                {
                    case "L":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
                        break;
                    case "M":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                        break;
                    case "Q":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
                        break;
                    case "H":
                        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
                        break;
                }
                Bitmap bm = null;
                bm = qrCodeEncoder.Encode("姓名:" + Model.TrueName + ",证书编号:12324,成绩合格,授予证书", Encoding.GetEncoding("GB2312"));
                //在指定的位置绘制生成的二维码
                gaphics.DrawImage(bm, new Rectangle(50, 530, 130, 125));
                #endregion
View Code
                #region 将生成的证书保存到指定目录

                string fileName = DateTime.Now.ToFileTime() + ".jpg";
                string filePath = System.Web.HttpContext.Current.Server.MapPath("UploadFile/");
                string SaveFile = filePath + fileName;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                    MemoryStream ms = new MemoryStream();
                    certificateBackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] buffer = ms.ToArray();
                    FileStream fs = new FileStream(SaveFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                    fs.Close();
                    fs.Dispose();
                }
                else
                {
                    MemoryStream ms = new MemoryStream();
                    certificateBackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] buffer = ms.ToArray();
                    FileStream fs = new FileStream(SaveFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                    fs.Close();
                    fs.Dispose();
                }
                #endregion
View Code

 

QRCode下载地址:http://down.51cto.com/data/758027
2013-5-14修改:不直接保存二维码到本地,使用GDI+将二维码画到证书模板后保存证书到指定位置。如果指定的目录不存在则创建目录。

你可能感兴趣的:(二维码)