.NET(C#)生成条形码
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#)生成条形码的更多相关文章
- C# 在Word文档中生成条形码
C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...
- 使用html2canvas实现批量生成条形码
/*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...
- JAVA生成条形码
1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...
- C# 生成条形码
原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 一.下载BarcodeLib.dll 下载地址 :http://do ...
- PHP5生成条形码器
前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...
- PHP生成条形码
前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...
- C# 利用BarcodeLib.dll生成条形码
首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...
- 使用PHP-Barcode轻松生成条形码(一)
最近由于工作需要,研究了一下PHP如何生成条形码.虽然二维码时下比较流行,但是条形码依然应用广泛,不可替代.园子里有很多讲利用PHP生成条形码的文章,基本上都是围绕Barcode Bakery的,它虽 ...
- java 生成条形码
package com.sun.erwei; import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;impo ...
随机推荐
- 即使连网了ping也会失败
/*************************************************************************** * 即使连网了ping也会失败 * 说明: * ...
- Open vSwitch FAQ (三)
Quality of Service (QoS) Q: How do I configure Quality of Service (QoS)? A: Suppose that you want to ...
- 67. Add Binary
public class Solution { public String addBinary(String a, String b) { char[] aa=a.toCharArray(); cha ...
- Codeforces Round #308 (Div. 2) A B C 水 数学
A. Vanya and Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- iOS8适配工作
1 按钮,菜单文字被加上下划线的问题. 2 状态栏被遮挡的问题.(iPhone6明显,iPhone4S无) 3 使用xcode6最新版本进行编译出现的通知无法呈现的问题(也不进行提示) 4 权限检测( ...
- leetcode 112 Path Sum ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 固定虚拟机的IP
- HTML初讲
整理老师所讲: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Promise 学习笔记
所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消息.Promise提供统一的API, ...
- JPA的Column注解总结
就像@Table注解用来标识实体类与数据表的对应关系类似,@Column注解来标识实体类中属性与数据表中字段的对应关系. 该注解的定义如下: @Target({METHOD, FIELD}) @Ret ...