Exemplo n.º 1
0
        public FileResult Index(string data)
        {
            var   steam = new System.IO.MemoryStream();
            Image image = QrCode.Generate(data);

            image.Save(steam, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Dispose();
            return(File(steam.ToArray(), "image/jpeg"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="data">内容</param>
        /// <param name="qrCodepath">二维码地址</param>
        /// <param name="tempPath">二维码地址无LOGO</param>
        /// <param name="logo">LOGO图</param>
        public static string GenerateQRCode(string data, string qrCodepath, string tempPath, Image logo = null)
        {
            try
            {
                Image image        = null;
                var   tempFilePath = HttpContext.Current.Request.MapPath(tempPath);
                var   fileInfo     = new FileInfo(tempFilePath);
                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }
                if (!string.IsNullOrWhiteSpace(tempPath) && fileInfo.Exists)
                {
                    FileStream fs = new FileStream(HttpContext.Current.Request.MapPath(tempPath), FileMode.Open, FileAccess.Read);
                    image = Image.FromStream(fs);
                    fs.Close();
                }
                else
                {
                    image = QrCode.Generate(data);
                    image.Save(HttpContext.Current.Request.MapPath(tempPath));
                }
                if (logo != null)
                {
                    image = QrCode.SetLogo(image, logo);
                }
                //保存
                string returnpath = HttpContext.Current.Request.MapPath(qrCodepath);

                image.Save(returnpath);
                image.Dispose();
                return(returnpath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }