• 利用免费服务生成QR二维码


        QR码越来越多的出现在了人们的日常生活中,比如 电影票、电子优惠券、甚至是电子机票。

        你能想象这样一张图片包含多少信息吗?

    ssss

        如今智能手机得到了普及,大多数手机也能读取QR码的信息。

        今天闲着无聊,写了一个小程序,用来生成QR码。程序很简单,没有使用到任何QR技术,不包含任何这方面的算法或者使用第三方的组件。只是简单的利用网络现有的免费资源。http://www.qrstuff.com/ 是国外的一个网站,它提供免费的QR生成服务。

        分析了一下源代码,发现只要通过 http://www.qrstuff.com/generate.generate?type=TEXT&text={0}&foreground_color=000000 这个地址就能生成指定内容的QR码 参数 text 指定为你所要的内容即可。

        程序很简单

    image

        就一个类

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CreateQR
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 产生QR码的地址
            /// </summary>
            readonly string qrcreateurl = "http://www.qrstuff.com/generate.generate?type=TEXT&text={0}&foreground_color=000000";
    
            private void btnSave_Click(object sender, EventArgs e)
            {
                //保存数据
                SaveData(picData);
            }
    
            /// <summary>
            /// 保存数据
            /// </summary>
            /// <param name="data"></param>
            private void SaveData(byte[] data)
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "PNG格式图片|*.png";
                saveFileDialog1.Title = "图片保存";
                saveFileDialog1.ShowDialog();
                if (saveFileDialog1.FileName != "")
                {
                    if (File.Exists(saveFileDialog1.FileName))
                    {
                        File.Delete(saveFileDialog1.FileName);
                    }
                    using (FileStream fs = File.Create(saveFileDialog1.FileName))
                    {
                        fs.Write(data, 0, data.Length);
                    }
                }
            }
    
            /// <summary>
            /// 临时图片数据
            /// </summary>
            private byte[] picData;
    
            private void btnCreate_Click(object sender, EventArgs e)
            {
                //下载
                DownloadData(string.Format(qrcreateurl,
                    string.IsNullOrEmpty(txtQR.Text) ? "无内容" : txtQR.Text),
                    (_send, _e) =>
                    {
                        picData = _e.Result;
                        using (MemoryStream ms = new System.IO.MemoryStream())
                        {
                            ms.Write(picData, 0, picData.Length);
                            pictureBox.Image = Image.FromStream(ms);
                        };
    
                    });
            }
    
            /// <summary>
            /// 下载数据
            /// </summary>
            /// <param name="url"></param>
            /// <param name="dceh"></param>
            private void DownloadData(string url, DownloadDataCompletedEventHandler dceh)
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadDataAsync(new Uri(url));
                    webClient.DownloadDataCompleted += dceh;
                };
    
            }
    
    
        }
    }

    文章最下面有全部源码以及demo 的下载地址

    点我下载Demo

    汤晓华 QQ 1881597 MSN tension1990@hotmail.com

    2011 05 22

  • 相关阅读:
    ECMAScript 6教程 (二) 对象和函数
    ECMAScript 6教程 (一)
    MongDB 批量更新
    Discuz模版与插件 安装时提示“对不起,您安装的不是正版应用...”解决方法
    命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)
    解决浮层弹出如何加上datepicker,并且浮动在上面
    Jquery DataTables warning : Requested unknown from the data source for row 0
    jquery.dataTables插件使用例子详解
    MVC @Html.DropDownListFor 默认值
    初探 ref 和 out
  • 原文地址:https://www.cnblogs.com/tandly/p/2053552.html
Copyright © 2020-2023  润新知