• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#生成彩色二维码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/// <summary>
        /// 生成彩色二维码
        /// </summary>
        /// <param name="data">二维码内容</param>
        /// <param name="filename">生成的图片名称(如:123.jpg)</param>
        /// <param name="filepath">图片存放路径(如:Images\\CustomerQRCode)</param>
        /// <returns></returns>
        public static bool CreateQRCode(string data, string filename, string filepath)
        {

            if (string.IsNullOrWhiteSpace(data) || string.IsNullOrWhiteSpace(filename) || string.IsNullOrWhiteSpace(filepath))
                return false;

            QRCodeWriter writer = new QRCodeWriter();
            Hashtable hints = new Hashtable();
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hints.Add(EncodeHintType.VERSION_START, 5);

            Bitmap image = writer.encode(data, BarcodeFormat.QR_CODE, 0x200, 0x200, hints).ToBitmap();//黑白二维码

            Bitmap bitmap2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(bitmap2);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.DrawImage(image, 0, 0);
            image.Dispose();

            Bitmap bitmap3 = QrCodeVertical(bitmap2.Width, bitmap2.Height);//彩色渐变二维码


            Color color = Color.FromArgb(200, 224, 114, 1);
            int num = 140;
            try
            {
                num -= (Encoding.UTF8.GetBytes(data).Length - 20) / 2;
            }
            catch (Exception)
            {
            }
            int num2 = num;
            int num3 = num2;
            for (int i = 0; i < bitmap2.Width; i++)
            {
                for (int j = 0; j < bitmap2.Height; j++)
                {
                    Color color3;
                    Color pixel = bitmap2.GetPixel(i, j);
                    if ((i < num2) && (j < num3))
                    {
                        color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel;
                    }
                    else
                    {
                        color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? bitmap3.GetPixel(i, j) : pixel;
                    }
                    bitmap2.SetPixel(i, j, color3);
                }
            }
            bitmap3.Dispose();

            #region 添加二维码标题
            string str2 = "";//标题一
            if (str2.Length < 1)
            {
                str2 = "MyTest";
            }
            float emSize = 32f;
            emSize -= (str2.Length - 4) * 1.8f;
            Font font = new Font("微软雅黑", emSize, FontStyle.Bold);
            SizeF ef = graphics.MeasureString(str2, font);
            float num7 = (bitmap2.Width - ef.Width) / 2f;
            Brush brush = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
            Brush brush2 = new SolidBrush(Color.White);
            int y = 50;
            graphics.FillRectangle(brush2, new Rectangle((int)num7, y, (int)ef.Width, (int)ef.Height));
            graphics.DrawString(str2, font, brush, (float)((int)num7), (float)y);
            Brush brush3 = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
            int width = 140;
            graphics.FillEllipse(brush2, (bitmap2.Width - width) / 2, (bitmap2.Height - width) / 2, width, width);
            int num10 = 0x80;
            graphics.FillEllipse(brush3, (bitmap2.Width - num10) / 2, (bitmap2.Height - num10) / 2, num10, num10);
            int num11 = 110;
            graphics.FillEllipse(brush2, (bitmap2.Width - num11) / 2, (bitmap2.Height - num11) / 2, num11, num11);

            #endregion

            #region 添加二维码中间内容 
            str2 = "";//标题二
            if (str2.Length < 1)
            {
                str2 = "Test";
            }
            float num12 = 32f;
            num12 -= (str2.Length - 3) * 3.5f;
            Font font2 = new Font("Meiryo", num12, FontStyle.Bold);
            ef = graphics.MeasureString(str2, font2);
            float x = ((bitmap2.Width - ef.Width) / 2f) + 2f;
            float num14 = ((bitmap2.Height - ef.Height) / 2f) + 8f;
            graphics.DrawString(str2, font2, brush3, x, num14);
            graphics.Dispose();
            #endregion


            filepath = System.Web.HttpContext.Current.Server.MapPath(@"~\"+ filepath) + "\\" + filename;

            //filepath = filepath + filename;
            System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            
            bitmap2.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

            fs.Close();
            image.Dispose();



            return true;

        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap