生成二维码与返回流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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.net\S2结业项目\MI\MI\img\logo-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

  

posted @   ZaraNet  阅读(566)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示