zoukankan      html  css  js  c++  java
  • C# CodeHelper

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BarcodeLib;
    using Gma.QrCodeNet.Encoding;
    using Gma.QrCodeNet.Encoding.Windows.Render;
    using System.Drawing.Imaging;
    using System.IO;
    
    
    namespace Utils
    {
        /// <summary>
        /// 条形码/二维码帮助类
        /// </summary>
        public class CodeHelper
        {
            /// <summary>
            /// 生成条形码
            /// </summary>
            /// <param name="Code"></param>
            public static void CreateBarCode(string Code, string fileName)
            {
                BarcodeLib.Barcode barcode = new BarcodeLib.Barcode()
                {
                    IncludeLabel = true,
                    Alignment = AlignmentPositions.CENTER,
                    Width = 300,
                    Height = 100,
                    RotateFlipType = RotateFlipType.RotateNoneFlipNone,
                    BackColor = Color.White,
                    ForeColor = Color.Black,
                };
    
                System.Drawing.Image img = barcode.Encode(TYPE.CODE128B, Code);
                img.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                //using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                //{
                    //img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //HttpContext.Current.Response.ClearContent();
                    //HttpContext.Current.Response.ContentType = "image/png";
                    //HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                //}
            }
    
            /// <summary>
            /// 生成二维码
            /// </summary>
            /// <param name="data">二维码内容</param>
            /// <param name="filename">生成的图片路径名称(如:D:123.png)</param>
            /// <param name="moduleSize">图片大小</param>
            /// <returns></returns>
            public static void CreateQRCode(string data, string filename, int moduleSize = 5)
            {
                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                QrCode qrCode = new QrCode();
                MemoryStream ms = new MemoryStream();
                qrEncoder.TryEncode(data, out qrCode);
    
                //GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two));
                //renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                //System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                //img.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
    
                GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
                using (FileStream stream = new FileStream(filename, FileMode.Create))
                {
                    render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
                }
            }
    
            /// <summary>
            /// 生成带Logo的二维码
            /// </summary>
            /// <param name="data"></param>
            /// <param name="filename"></param>
            /// <param name="logPath"></param>
            /// <param name="moduleSize"></param>
            public static void CreateQRCodeHasLog(string data, string filename, string logPath, int moduleSize = 5)
            {
                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
                QrCode qrCode = qrEncoder.Encode(data);
                //保存成png文件
                GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, 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图片大小和二维码大小的比例
                System.Drawing.Image img = System.Drawing.Image.FromFile(logPath);
                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);
            }
        }
    }
    View Code
  • 相关阅读:
    Windows XP下 Android开发环境 搭建
    Android程序的入口点
    在eclipse里 新建android项目时 提示找不到proguard.cfg
    64位WIN7系统 下 搭建Android开发环境
    在eclipse里 新建android项目时 提示找不到proguard.cfg
    This Android SDK requires Android Developer Toolkit version 20.0.0 or above
    This Android SDK requires Android Developer Toolkit version 20.0.0 or above
    Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead
    Windows XP下 Android开发环境 搭建
    Android程序的入口点
  • 原文地址:https://www.cnblogs.com/ariter/p/6112595.html
Copyright © 2011-2022 走看看