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();
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM