QRcode生成二维码,保存二维码图片到服务器

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Text.RegularExpressions;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using ThoughtWorks;
using ThoughtWorks.QRCode;
using DataAccess;
using System.Data;
using System.IO;
namespace QFMobileService
{//测试http://192.168.1.102:8001/GetQR.aspx?RowGuid=696d5504-2296-4ac4-8aa8-f111f39aaw22
    public partial class GetQR : System.Web.UI.Page
    {
        string rowguid = "";
        C_Product_QRInfo cpqr = new C_Product_QRInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            rowguid = Request["RowGuid"] != null ? Request["RowGuid"].ToString() : "" ;
            if (!string.IsNullOrEmpty(rowguid))
            {
                cpqr.GetModel(rowguid);
                create_two(rowguid);
                initQRInfo();
            }
        }
        private void create_two(string nr)
        {
            Bitmap bt;
            string enCodeString = nr;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
            string filename = string.Format(DateTime.Now.ToString(), "yyyymmddhhmmss");
            filename = filename.Replace(" ", "");
            filename = filename.Replace(":", "");
            filename = filename.Replace("-", "");
            filename = filename.Replace(".", "");
            filename += ".jpg";
            //文件夹是否存在
            string path = Server.MapPath("~/QRimage/");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            #region 测试代码
                 //bt.Save(HttpContext.Current.Server.MapPath("~/QRimage/") + filename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            string spath = path + filename;
            Save(bt, 100, path+filename);
            #endregion
            //bt.Save(Server.MapPath("~/QRimage/") + filename + ".jpg");
            this.Image1.ImageUrl = "~/QRimage/" + filename;
        }
        #region OA的上传图片
       /// <summary>
        /// 保存图片。
        /// </summary>
        /// <param name="image">要保存的图片</param>
        /// <param name="quality">品质(1L~100L之间,数值越大品质越好)</param>
        /// <param name="filename">保存路径</param>
        public static void Save(Bitmap image, long quality, string filename)
        {
            using (EncoderParameters encoderParams = new EncoderParameters(1))
            {
                using (EncoderParameter parameter = (encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)))
                {
                    ImageCodecInfo encoder = null;
                    //取得扩展名
                    string ext = Path.GetExtension(filename);
                    if (string.IsNullOrEmpty(ext))
                        ext = ".jpg";
                    //根据扩展名得到解码、编码器
                    foreach (ImageCodecInfo codecInfo in ImageCodecInfo.GetImageEncoders())
                    {
                        if (Regex.IsMatch(codecInfo.FilenameExtension, string.Format(@"(;|^)*{0}(;|$)", ext), RegexOptions.IgnoreCase))
                        {
                            encoder = codecInfo;
                            break;
                        }
                    }
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    image.Save(filename, encoder, encoderParams);
                }
            }
        }
        #endregion
        void initQRInfo()
        {
            D_Product_QRInfo dpqr = new D_Product_QRInfo();
            DataTable dt =dpqr.GetQRInfo(rowguid);
            if (null == dt || dt.Rows.Count <= 0)
            {
                return;
            }
            else
            {
                td_OrderNumber.InnerText = dt.Rows[0]["OrderNumber"].ToString();
                td_Batch.InnerText = dt.Rows[0]["Batch"].ToString();
                td_Number.InnerText = dt.Rows[0]["Number"].ToString();
                DateTime date;
                if (null != dt.Rows[0]["CreateTime"] && DateTime.TryParse(dt.Rows[0]["CreateTime"].ToString(), out date))
                {
                    td_DateTime.InnerText = date.ToShortDateString();
                }
                td_GoodsDanWei.InnerText = dt.Rows[0]["GoodsDanWei"].ToString();
                td_GoodsGuiGe.InnerText = dt.Rows[0]["GoodsGuiGe"].ToString();
                td_GoodsName.InnerText = dt.Rows[0]["GoodsName"].ToString();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/sulong/p/4892654.html