.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 ...
随机推荐
- 谈 IIS7.5 Asp.Net模拟用户
IIS Asp.模拟用户官方的解释是: 如果要在非默认安全上下文中运行 ASP.NET 应用程序,请使用 ASP.NET 模拟身份验证. 如果您对某个 ASP.NET 应用程序启用了模拟,那么该应用 ...
- 关于string的练习题目
/*Are they equal*/#include<iostream>#include<string>using namespace std;int n;string dea ...
- HDU 5918 KMP/模拟
Sequence I Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- keil uvision看厌了么?试试Sublime Text吧!
之前用 Sublime Text(以下简称 ST )配置了 C/C++ 开发环境,感觉相当不错,作为编辑器的 ST,编辑代码的功能当然是相当棒的,美中不足的是目前只能编译单个文件,但是用来做些小练习也 ...
- 使用isInEditMode解决可视化编辑器无法识别自定义控件的问题
如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码, 会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to s ...
- MATLAB符号运算
1.符号运算 使用MATLAB可以进行多项式乘除运算,也可以进行因式分解. 例1. 多项式乘除运算(x+3)3 >> syms x;>> expand((x+3)^3) ans ...
- Baxter机器人---Hello_baster(二)
原创博文,转载请标明出处:--周学伟http://www.cnblogs.com/zxouxuewei/ Step 1: Setup ROS Environment root@zxwubuntu-As ...
- python3获取当前目录(转)
转自:http://www.elias.cn/Python/GetPythonPath?from=Develop.GetPythonPath 1. 以前的方法 如果是要获得程序运行的当前目录所在位置 ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- POJ 1043 What's In A Name?(唯一的最大匹配方法)
What's In A Name? Time Limit: 1000MS Memor ...