一维码生成 c# winform GUI
最近看到同事小红在做一维码,感觉挺好玩,于是就在网上找了一个例子来玩玩。



.png)
/// <summary>
/// 生成条码 Bitmap,自定义条码高度,自定义文字对齐样式
/// </summary>
/// <param name="sourceCode"></param>
/// <param name="barCodeHeight"></param>
/// <param name="sf"></param>
/// <returns></returns>
public Bitmap GetCode39(string sourceCode, int barCodeHeight, StringFormat sf)
{
BarCodeText = sourceCode.ToUpper();
int leftMargin = ;
int topMargin = ;
int thickLength = ;
int narrowLength = ;
int intSourceLength = sourceCode.Length;
string strEncode = "" ; //添加起始码“ *”.
var font = new System.Drawing.Font( "Segoe UI", );
string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*" ;
string[] Code39 =
{
/* 0 */ "" ,
/* 1 */ "" ,
/* 2 */ "" ,
/* 3 */ "" ,
/* 4 */ "" ,
/* 5 */ "" ,
/* 6 */ "" ,
/* 7 */ "" ,
/* 8 */ "" ,
/* 9 */ "" ,
/* A */ "" ,
/* B */ "" ,
/* C */ "" ,
/* D */ "" ,
/* E */ "" ,
/* F */ "" ,
/* G */ "" ,
/* H */ "" ,
/* I */ "" ,
/* J */ "" ,
/* K */ "" ,
/* L */ "" ,
/* M */ "" ,
/* N */ "" ,
/* O */ "" ,
/* P */ "" ,
/* Q */ "" ,
/* R */ "" ,
/* S */ "" ,
/* T */ "" ,
/* U */ "" ,
/* V */ "" ,
/* W */ "" ,
/* X */ "" ,
/* Y */ "" ,
/* Z */ "" ,
/* - */ "" ,
/* . */ "" ,
/*' '*/ "" ,
/* $ */ "" ,
/* / */ "" ,
/* + */ "" ,
/* % */ "" ,
/* * */ ""
};
sourceCode = sourceCode.ToUpper();
Bitmap objBitmap = new Bitmap(((thickLength * + narrowLength * ) * (intSourceLength + )) +
(leftMargin * ), barCodeHeight + (topMargin * ));
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle( Brushes.White, , , objBitmap.Width, objBitmap.Height);
for (int i = ; i < intSourceLength; i++)
{
//非法字符校验
if (AlphaBet.IndexOf(sourceCode[i]) == - || sourceCode[i] == '*' )
{
objGraphics.DrawString( "Invalid Bar Code", SystemFonts.DefaultFont, Brushes .Red, leftMargin, topMargin);
return objBitmap;
}
//编码
strEncode = string.Format("{0}0{1}" , strEncode,
Code39[AlphaBet.IndexOf(sourceCode[i])]);
}
strEncode = string.Format("{0}0010010100" , strEncode); //添加结束码“*”
int intEncodeLength = strEncode.Length;
int intBarWidth;
for (int i = ; i < intEncodeLength; i++) //绘制 Code39 barcode
{
intBarWidth = strEncode[i] == '' ? thickLength : narrowLength;
objGraphics.FillRectangle(i % == ? Brushes.Black : Brushes .White, leftMargin, topMargin, intBarWidth, barCodeHeight);
leftMargin += intBarWidth;
}
//绘制明码
Font barCodeTextFont = new Font( "黑体" , 10F);
RectangleF rect = new RectangleF(, barCodeHeight - , objBitmap.Width - , );
objGraphics.FillRectangle( Brushes.White, rect);
//文本对齐
objGraphics.DrawString(BarCodeText, barCodeTextFont, Brushes.Black, rect, sf);
return objBitmap;
}
好了,如果有兴趣的同学就可以去看看怎么实现自己的想要的一维码了。
一维码生成 c# winform GUI的更多相关文章
- C# 一维码生成
概念 一维条码即指条码条和空的排列规则,常用的一维码的码制包括:EAN码.39码.交叉25码.UPC码.128码.93码,ISBN码,及Codabar(库德巴码)等. 条形码起源于 20 世纪 40 ...
- 使用Zxing 一维码
最近看到满大街的二维码扫码有惊喜,对二维码也有过一些了解,想看看到底是什么原理,在网上找了一些资料,自己弄了一个实例,采用的是MVC,贴出来分享一下 一维码生成 Controller public A ...
- winform使用Barcodex控件预览和打印一维码
1.控件下载. http://files.cnblogs.com/files/masonblog/barcodex.zip . 包含barcodex.ocx控件.barcodex帮助文档.两个winf ...
- Android生成一维码
BitmapUtil.java里面添加个方法 /** * 用于将给定的内容生成成一维码 注:目前生成内容为中文的话将直接报错,要修改底层jar包的内容 * * @param content 将要生成一 ...
- Winform窗体实现简单的二维码生成和保存
二维码的生成需要用到二维码生成的类库,ThoughtWorks.QRCode.dll 步骤: 第一步:下载二维码生成类库,ThoughtWorks.QRCode.dll 第二步:新建winform项目 ...
- C# 使用ZXing.NET生成一维码、二维码
以上图片是本示例中的实际运行效果,在生活中我们的一维码(也就是条形码).二维码 使用已经非常广泛,那么如何使用c#.net来进行生成一维码(条形码).二维码呢? 使用ZXing来生成是非常方便的选择, ...
- zxing解析生成一维码二维码
@web界面实现扫一扫 二维码工具类 package util; import java.awt.BasicStroke; import java.awt.Graphics; import java. ...
- android二维码生成
前生: 一维码:条形码 数字 缺点:不好看,占面积, 好了,请看效果图: 在准备之前我们要导一个包:core-3.2.1.jar 下载请访问: http://download.csdn.net/do ...
- zxing 一维码部分深入分析与实际应用,识别卡片数量,Android数卡器
打算修改zxing 源码应用到其它方面,所以最近花了点时间阅读其源码,无意中找到这篇博客,条码扫描二维码扫描——ZXing android 简化源码分析 对过程的分析还是可以参考的.原作者给出的一个基 ...
随机推荐
- Centos启动Cassandra交互模式失败:No appropriate python interpreter found
在CentOS6.5安装好Cassandra后,启动交互模式: bin/sqlsh 192.168.10.154 时,报错 No appropriate python interpreter foun ...
- 虚拟化--IO虚拟化基本原理
本文话题: IO虚拟化概述 设备发现 访问截获 设备模拟 设备共享基于软件的IO虚拟化 基于前端后端的IO虚拟化基于硬件的IO虚拟化 概述 从处理器的角度看,外设是通过一组I/O资源(端口I/O或者是 ...
- oracle 中数据库完全导入导出:cmd命令行模式(转载)
http://www.3lian.com/edu/2012/12-01/47252.html Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库 ...
- DataFormatString 转
数据绑定之DataFormatString 设定BoundField的DataFormatString,通常有以下几种 DataFormatString= "{0:C}" 货币,货 ...
- Mvc 提交表单的4种方法全程详解
一,MVC HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){} BeginRouteForm ...
- Custom Web Servic In MOSS 2007
Tools: Visual Studio 2008,Visual Studio 2008 Command Prompt, Sharepoint Server 2007 Generate .disco ...
- guava学习--Function、Predicate
Function用于同步转换. Predicate用于过滤. import java.util.Collection; import java.util.Iterator; import java.u ...
- PHP发送电子邮件
1.导入文件,如本案例把Stmp.class.php放到Common\Common目录下,代码很多,直接复制就行! <?php namespace Common\Common; class Sm ...
- 数据类型、常量、变量、printf、scanf和运算符
数据类型 常量 变量 printf函数介绍 scanf函数介绍 运算符 数据类型 数据类型是指数据在内存中存储的方式. C语言中有5大数据类型:基本类型.构造类型.指针类型.空类型.定义类型. C语言 ...
- Scala 的确棒
我的确认为计算机学院应该开一门 Scala 的语言课程. 在这篇文章中,我会讲述为什么我会有这样的想法,在此之前,有几点我想要先声明一下: 本文无意对编程语言进行评比,我要讲述的主体是为什么你应该学习 ...