码迷,mamicode.com
首页 > Windows程序 > 详细

C# Gma.QrCodeNet生成二维码

时间:2016-04-23 22:54:57      阅读:1169      评论:0      收藏:0      [点我收藏+]

标签:

一.使用C#生成二维码类库之一是qrcodenet

源代码地址:

http://qrcodenet.codeplex.com/

二.使用实例

定义处理字符串

static string url = "http://blog.csdn.net/u011127019";

1.实例1

//生成二维码
static void Generate1()
{
    //创建二维码生成类
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode(url);
    //输出显示在控制台
    for (int j = 0; j < qrCode.Matrix.Height; j++)
    {
        for (int i = 0; i < qrCode.Matrix.Width; i++)
        {
            char charToPoint = qrCode.Matrix[i, j] ?  :  ;
            Console.Write(charToPoint);
        }
        Console.WriteLine();
    }
}

2.生成二维码,保存成图片

//生成图片
static void Generate2()
{
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode(url);
    //保存成png文件
    string filename = @"H:\桌面\截图\url.png";
    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
    using (FileStream stream = new FileStream(filename, FileMode.Create))
    {
        render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
    }
}

技术分享

3.支持中文

//生成中文二维码
static void Generate3()
{
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode("我是小天马");
    //保存成png文件
    string filename = @"H:\桌面\截图\cn.png";
    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

    Bitmap map = new Bitmap(500, 500);
    Graphics g = Graphics.FromImage(map);
    g.FillRectangle(Brushes.Red, 0, 0, 500, 500);
    render.Draw(g, qrCode.Matrix, new Point(20, 20));
    map.Save(filename, ImageFormat.Png);
}

4.调整二维码大小

//设置二维码大小
static void Generate4()
{
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode("我是小天马");
    //保存成png文件
    string filename = @"H:\桌面\截图\size.png";
    //ModuleSize 设置图片大小  
    //QuietZoneModules 设置周边padding
    /*
        * 5----150*150    padding:5
        * 10----300*300   padding:10
        */
    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two), Brushes.Black, Brushes.White);

    Point padding = new Point(10, 10);
    DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
    Bitmap map = new Bitmap(dSize.CodeWidth + padding.X, dSize.CodeWidth + padding.Y);
    Graphics g = Graphics.FromImage(map);
    render.Draw(g, qrCode.Matrix, padding);
    map.Save(filename, ImageFormat.Png);
}

技术分享

5.生成带Logo的二维码

//生成带Logo的二维码
static void Generate5()
{
    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode = qrEncoder.Encode("我是小天马");
    //保存成png文件
    string filename = @"H:\桌面\截图\logo.png";
    GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

    DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
    Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
    Graphics g = Graphics.FromImage(map);
    render.Draw(g, qrCode.Matrix);
    //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例
    Image img = Image.FromFile(@"F:\JavaScript_Solution\QrCode\QrCode\Images\101.jpg");

    Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2);
    g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height);
    map.Save(filename, ImageFormat.Png);
}
技术分享

C# Gma.QrCodeNet生成二维码

标签:

原文地址:http://www.cnblogs.com/tianma3798/p/5425726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!