zoukankan      html  css  js  c++  java
  • 生成二维码与返回流

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Web;
    
    namespace Glonee.Handler
    {
        /// <summary>
        /// ErWeiMa 的摘要说明
        /// </summary>
        public class ErWeiMa : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string strSource = context.Request.Params["strSource"];
                string text = "";
                int width = 250;
                int height = 250;
                string fontName = "楷体";
                Image image = (GetTwoDimensionCode(strSource,text,width,height,fontName));
                context.Response.ContentType = "image/jpeg";
                image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                image.Dispose();
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            public static Bitmap GetTwoDimensionCode(string strSource,
               string text, int width, int height, string fontName)
            {
                // 创建Bitmap对象
                Bitmap bmp = new Bitmap(width, height);
                // 从image创建 Graphics对象
                Graphics objGraphics = Graphics.FromImage(bmp);
                // 填上背景色
                objGraphics.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
                // 
                ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder =
                    new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
                // 设置编码方法
                qrCodeEncoder.QRCodeEncodeMode =
                    ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
                // 设置大小
                qrCodeEncoder.QRCodeScale = 3;
                // 适用于信息量较少的情形,图像越小保存的信息量越少
                // qrCodeEncoder.QRCodeScale = 4;
                // 设置版本
                qrCodeEncoder.QRCodeVersion = 0;
                // 设置错误校验的级别,正因为二维码有纠错能力,才能够加入logo
                qrCodeEncoder.QRCodeErrorCorrect =
                    ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;
                Image image = qrCodeEncoder.Encode(strSource, Encoding.GetEncoding("utf-8"));
                // 写入二维码
                int x = (int)(width - image.Width) / 2;
                int y = (int)(height - image.Height) / 2;
                objGraphics.DrawImage(image, new Point(x, y));
    // 添加Logo图标
                image = Image.FromFile(@"G:ASP.netS2结业项目MIMIimglogo-footer.png");
                x = (int)(width - image.Width) / 2;
                y = (int)(height - image.Height) / 2;
                objGraphics.DrawImage(image, new Point(x, y));
                // 写入字符串
                //objGraphics.DrawString(text, new Font(fontName, 13, FontStyle.Bold), 
                //    Brushes.Black, new PointF(43, 15));
                return bmp;
            }
        }
    }
    
    
    想要使用这个返回的值 就直接src 去访问这个handler
    

      

  • 相关阅读:
    C# 多线程并发锁模式-总结
    C# 7 out variables, tuples & other new features
    AngleSharp 的Dom 选择器
    Html Agility Pack
    javascript判断是否按回车键
    VSTS 免费代码git/tfs托管体验-使用代码云托管
    NPOI 中的公式列的值的获取
    topshelf 开发windows 服务资料
    vue之指令
    hash和md5
  • 原文地址:https://www.cnblogs.com/ZaraNet/p/9434028.html
Copyright © 2011-2022 走看看