C#生成Bar Code Image

public class Barcode : IHttpHandler
    {
        private object _code = "";
        private int _height = 52;
        private string code = "";
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["code"] != null)
            {
                _code = context.Request.QueryString["code"].ToString();
                _code = "*" + _code + "*";
            }
            code = bar_code(_code);
            int p_w = code.Length;
   //int p_h = _height + 20;
   int p_h = _height;
            context.Response.ContentType = "image/gif";
            Bitmap myBitmap = new Bitmap(p_w, p_h);
            Graphics myGrap = Graphics.FromImage(myBitmap);
            myGrap.Clear(Color.White);
            for (int i = 0; i < p_w; i++)
            {
                Pen myPen = new Pen(Color.White, 1);
                if (code.Substring(i, 1) == "|")
                {
                    myPen.Color = Color.Black;
                }
                myGrap.DrawLine(myPen, i, 0, i, _height);
            }
   //myGrap.DrawString(_code.ToString(), new Font("Courier New", 10), new SolidBrush(Color.Black), -4, _height);
            myBitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
            context.Response.End();
        }
        public string bar_code(object str)
        {
            string strTmp = str.ToString();
            string code = strTmp;
            strTmp = strTmp.ToLower();
            strTmp = strTmp.Replace("0", "_|_|__||_||_|");
            strTmp = strTmp.Replace("1", "_||_|__|_|_||");
            strTmp = strTmp.Replace("2", "_|_||__|_|_||");
            strTmp = strTmp.Replace("3", "_||_||__|_|_|");
            strTmp = strTmp.Replace("4", "_|_|__||_|_||");
            strTmp = strTmp.Replace("5", "_||_|__||_|_|");
            strTmp = strTmp.Replace("7", "_|_|__|_||_||");
            strTmp = strTmp.Replace("6", "_|_||__||_|_|");
            strTmp = strTmp.Replace("8", "_||_|__|_||_|");
            strTmp = strTmp.Replace("9", "_|_||__|_||_|");
            strTmp = strTmp.Replace("a", "_||_|_|__|_||");
            strTmp = strTmp.Replace("b", "_|_||_|__|_||");
            strTmp = strTmp.Replace("c", "_||_||_|__|_|");
            strTmp = strTmp.Replace("d", "_|_|_||__|_||");
            strTmp = strTmp.Replace("e", "_||_|_||__|_|");
            strTmp = strTmp.Replace("f", "_|_||_||__|_|");
            strTmp = strTmp.Replace("g", "_|_|_|__||_||");
            strTmp = strTmp.Replace("h", "_||_|_|__||_|");
            strTmp = strTmp.Replace("i", "_|_||_|__||_|");
            strTmp = strTmp.Replace("j", "_|_|_||__||_|");
            strTmp = strTmp.Replace("k", "_||_|_|_|__||");
            strTmp = strTmp.Replace("l", "_|_||_|_|__||");
            strTmp = strTmp.Replace("m", "_||_||_|_|__|");
            strTmp = strTmp.Replace("n", "_|_|_||_|__||");
            strTmp = strTmp.Replace("o", "_||_|_||_|__|");
            strTmp = strTmp.Replace("p", "_|_||_||_|__|");
            strTmp = strTmp.Replace("r", "_||_|_|_||__|");
            strTmp = strTmp.Replace("q", "_|_|_|_||__||");
            strTmp = strTmp.Replace("s", "_|_||_|_||__|");
            strTmp = strTmp.Replace("t", "_|_|_||_||__|");
            strTmp = strTmp.Replace("u", "_||__|_|_|_||");
            strTmp = strTmp.Replace("v", "_|__||_|_|_||");
            strTmp = strTmp.Replace("w", "_||__||_|_|_|");
            strTmp = strTmp.Replace("x", "_|__|_||_|_||");
            strTmp = strTmp.Replace("y", "_||__|_||_|_|");
            strTmp = strTmp.Replace("z", "_|__||_||_|_|");
            strTmp = strTmp.Replace("-", "_|__|_|_||_||");
            strTmp = strTmp.Replace("*", "_|__|_||_||_|");
            strTmp = strTmp.Replace("/", "_|__|__|_|__|");
            strTmp = strTmp.Replace("%", "_|_|__|__|__|");
            strTmp = strTmp.Replace("+", "_|__|_|__|__|");
            strTmp = strTmp.Replace(".", "_||__|_|_||_|");
            return strTmp;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
时间: 2024-10-07 20:03:11

C#生成Bar Code Image的相关文章

Demo on bar code printing using SAP Scripts/Smart forms

This document will explain the printing of bar code using SAP Scripts/Smart forms Target Readers: SAP-ABAP consultants with knowledge of layout designing. Definition:  Bar codes are often printed on labels to allow machine to read the data. SAP has p

添加条码到WPF文本的条形码控件Actipro Bar Code

最近发现MDT推出去的系统的有不同问题,其问题就不说了,主要是策略权限被域继承了.比如我们手动安装的很多东东都是未配置壮态,推的就默认为安全壮态了,今天细找了一下,原来把这个关了就可以了. 添加条码到WPF文本的条形码控件Actipro Bar Code

EChart - 根据Json返回值生成Bar图

根据Json结果集生成Bar图, 第一列固定为描述, 后面的列为值字段. <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</title> </head> <body> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="he

C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

方法很多,可以参考以下几种方案,就不展开,地址如下: https://www.cnblogs.com/mingmingruyuedlut/p/6120671.html 这里描述的,是某项目需求,在网页中输出为图片并展示,代码如下: using ThoughtWorks.QRCode.Codec; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; s

VS2015 C#利用QrCodeNet生成QR Code

Step by step Create QR Code with QrCodeNet Step.1 新建項目 Step.2 在窗口中拖入一個Button Step.3 下載QrCodeNet代碼,解壓\QrCodeNet\sourceCode\qrcodenet\Gma.QrCodeNet\nuget\lib\net45\Gma.QrCodeNet.Encoding.dll Step.4 加入參考(引用) Step.5 添中using using Gma.QrCodeNet.Encoding;

Myeclipse中生成subscription code的代码

//代码如下: package com.qls.AddingMethodsToAnEnum; import java.io.*; public class MyEclipseGen { private static final String LL = "Decompiling this copyrighted software is a violation of both your license agreement and the Digital Millenium Copyright Act

Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例

这个实例在windows.OS X.IOS和Android等平台运行正常.本文参考这个网站提供的方法:http://zarko-gajic.iz.hr/firemonkey-mobile-android-ios-qr-code-generation-using-delphi-xe-5-delphizxingqrcode/ 代码中用到的DelphiZXingQRCode.Pas点这下载 1 unit Unit3; 2 3 interface 4 5 uses 6 System.SysUtils,

php和jquery生成QR Code

php生产QR Code 下载qrcode源码,地址:https://sourceforge.net/projects/phpqrcode/files/releases/ 1.解压后引入qrlib.php文件 2.使用 QRcode::png($url); png这个函数的几个参数解释下 png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) $text:二维

(2-Dimensional Bar Code)深色

http://pic.cnhubei.com/space.php?uid=2289&do=album&id=967003 http://pic.cnhubei.com/space.php?uid=2289&do=album&id=967004 http://pic.cnhubei.com/space.php?uid=2289&do=album&id=967006 http://pic.cnhubei.com/space.php?uid=2289&do