using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(bar_code("www.sosuo8.com", , , ));
}
public string bar_code(object str, int ch, int cw, int type_code)
{
//str:输入的字符串;ch:要显示条形码的高度;cw:要显示条形码的宽度;type_code:代码类型
string strTmp = str.ToString();
string code = strTmp;
// ToLower()将string转化成小写形式的副本,返回是使用指定区域的性的大小写规则。
strTmp = strTmp.ToLower();
int height = ch;
int width = cw; //将传入的参数进行转化。
strTmp = strTmp.Replace("", "_|_|__||_||_|"); ;
strTmp = strTmp.Replace("", "_||_|__|_|_||");
strTmp = strTmp.Replace("", "_|_||__|_|_||");
strTmp = strTmp.Replace("", "_||_||__|_|_|");
strTmp = strTmp.Replace("", "_|_|__||_|_||");
strTmp = strTmp.Replace("", "_||_|__||_|_|");
strTmp = strTmp.Replace("", "_|_|__|_||_||");
strTmp = strTmp.Replace("", "_|_||__||_|_|");
strTmp = strTmp.Replace("", "_||_|__|_||_|");
strTmp = strTmp.Replace("", "_|_||__|_||_|");
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(".", "_||__|_|_||_|");
strTmp = strTmp.Replace("_", "<span style='height:" + height + ";width:" + width + ";background:#FFFFFF;'></span>");
strTmp = strTmp.Replace("|", "<span style='height:" + height + ";width:" + width + ";background:#000000;'></span>"); if (type_code == )
{
return strTmp + "<BR>" + code;
}
else
{
return strTmp;
}
}
} 方法二:
using System.Drawing; public void CreateCodeLogo(string code)
{ long len = code.Length;
string lastString = "";
char[] list = new char[len + ]; list = code.ToCharArray(); for (int i = ; i < list.Length; i++)
{
lastString += this.ConvertToBinaryString(list[i].ToString());
} char[] numList = new char[lastString.Length + ];
numList = lastString.ToCharArray(); Bitmap image = new Bitmap(, );
Graphics g = Graphics.FromImage(image); g.Clear(Color.White); Pen penBlack = new Pen(Color.FromArgb(, , , ), 2.5F);
Pen penWhite = new Pen(Color.White, 2.5F); int j = ; for (float k = ; j < numList.Length; k += 2F, j++)
{
if (numList[j].ToString() == "")
{
g.DrawLine(penBlack, k, , k, ); }
else
{
g.DrawLine(penWhite, k, , k, );
} if (j % == )
{
g.DrawString(list[j / ].ToString(), new System.Drawing.Font("Courier New", ), new SolidBrush(Color.Red), k, );
}
}
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
} //将字符串数值转换为二进制字符串数值
public string ConvertToBinaryString(string buf)
{
int[] temp = new int[];
string binary;
int val = , i = , j; //先将字符转化为十进制数
try
{
val = Convert.ToInt32(buf);
}
catch
{
val = ;
} if (val == )
{
return ("");
} i = ;
while (val != )
{
temp[i++] = val % ;
val /= ;
} binary = ""; for (j = ; j <= i - ; j++)
{
binary += (char)(temp[i - j - ] + );
} if (binary.Length < ) //如果小于4位左边补零
{
int len = - binary.Length;
string str = ""; while (len > )
{
str += "";
len--;
} binary = str + binary;
} return (binary);
} protected void Button1_Click(object sender, EventArgs e)
{
CreateCodeLogo(TextBox1.Text);
}

.NET(C#)生成条形码的更多相关文章

  1. C# 在Word文档中生成条形码

    C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...

  2. 使用html2canvas实现批量生成条形码

    /*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...

  3. JAVA生成条形码

    1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...

  4. C# 生成条形码

    原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...

  5. C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)

    原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 一.下载BarcodeLib.dll 下载地址 :http://do ...

  6. PHP5生成条形码器

    前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...

  7. PHP生成条形码

    前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...

  8. C# 利用BarcodeLib.dll生成条形码

    首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...

  9. 使用PHP-Barcode轻松生成条形码(一)

    最近由于工作需要,研究了一下PHP如何生成条形码.虽然二维码时下比较流行,但是条形码依然应用广泛,不可替代.园子里有很多讲利用PHP生成条形码的文章,基本上都是围绕Barcode Bakery的,它虽 ...

  10. java 生成条形码

    package com.sun.erwei; import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;impo ...

随机推荐

  1. 《JS高程》基本类型和引用类型的值学习笔记

    ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值. 创建方式类似:创建一个变量并为其赋值. (1)基本类型值和引用类型值比较 基本类型值 引用类型值 简单的数据段 可能由 ...

  2. 【题解】【直方图】【Leetcode】Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. Spark延长SparkContext初始化时间

    有些应用中可能希望先在driver上运行一段java单机程序,然后再初始化SparkContext用集群模式操作java程序返回值.从而避免过早建立SparkContext对象分配集群资源,使资源长时 ...

  4. OpenFlow Switch学习笔记(七)——Matching Fields

    Matching Fields in_port=port Matches OpenFlow port port dl_vlan=vlan Matches IEEE 802.1q Virtual LAN ...

  5. httpclient 发送一个请求

    httpclient版本 4.1 发送一个post请求 public static JSONObject post(String url,JSONObject json){ HttpClient cl ...

  6. Android内存管理机制之一:low memory killer

    转载自http://www.miui.com/thread-29268-1-1.html 准备写这个专题之前,心里是有点忐忑的.首先Android内存管理机制相当复杂,想要讲清楚比较困难:其次对于绝大 ...

  7. HDFS简介【全面讲解】

    http://www.cnblogs.com/chinacloud/archive/2010/12/03/1895369.html [一]HDFS简介HDFS的基本概念1.1.数据块(block)HD ...

  8. linux安装sqlcmd登录sqlserver

    首先从微软网站下载sqlncli安装文件,link. 因为是在内网安装,首先手工下载unixODBC2.3.0.tar.gz,下载后上传到服务器. 将下载的tar文件文件,放在同build_dm.sh ...

  9. UVa 10037 - Bridge

    题目大意 在一个晚上有N个人过河,他们有一个手电筒,需要有手电筒才能过河,每次最多两个人同时过河,每次过河时间等于速度最慢的那个人的过河时间,让所有人全部过河,花费的时间最少是多少? 分析 如果只有一 ...

  10. 安装Python+Pywin32(version 3.3)

    1.下载python3.3,默认设置,安装. 2.完成后,在开始-程序中运行python IDLE.我在运行时出现了应用程序运行异常,原因是与其他软件内存发生冲突,如.net framework等. ...