BarCode條碼基於C# GDI+ 的實現方法詳解

來源:互聯網
上載者:User

條碼在生活中的應用非常廣泛,具體的條碼知識大家自行百度,瞭解條碼知識對理解下面的代碼是必要的。如果只是應用的話,直接拿去就可以用了。
廢話不多說,上代碼

複製代碼 代碼如下:public Bitmap GetCode39(string sourceCode)
{
int leftMargin = 5;
int topMargin = 0;
int thickLength = 2;
int narrowLength = 1;
int barCodeHeight = 35;
int intSourceLength = sourceCode.Length;
string strEncode = "010010100"; //添加起始碼“*”.
var font = new System.Drawing.Font("Segoe UI", 5);

string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";

string[] Code39 =
{
/* 0 */ "000110100",
/* 1 */ "100100001",
/* 2 */ "001100001",
/* 3 */ "101100000",
/* 4 */ "000110001",
/* 5 */ "100110000",
/* 6 */ "001110000",
/* 7 */ "000100101",
/* 8 */ "100100100",
/* 9 */ "001100100",
/* A */ "100001001",
/* B */ "001001001",
/* C */ "101001000",
/* D */ "000011001",
/* E */ "100011000",
/* F */ "001011000",
/* G */ "000001101",
/* H */ "100001100",
/* I */ "001001100",
/* J */ "000011100",
/* K */ "100000011",
/* L */ "001000011",
/* M */ "101000010",
/* N */ "000010011",
/* O */ "100010010",
/* P */ "001010010",
/* Q */ "000000111",
/* R */ "100000110",
/* S */ "001000110",
/* T */ "000010110",
/* U */ "110000001",
/* V */ "011000001",
/* W */ "111000000",
/* X */ "010010001",
/* Y */ "110010000",
/* Z */ "011010000",
/* - */ "010000101",
/* . */ "110000100",
/*' '*/ "011000100",
/* $ */ "010101000",
/* / */ "010100010",
/* + */ "010001010",
/* % */ "000101010",
/* * */ "010010100"
};
sourceCode = sourceCode.ToUpper();

Bitmap objBitmap = new Bitmap(
((thickLength * 3 + narrowLength * 7) * (intSourceLength + 2)) + (leftMargin * 2),
barCodeHeight + (topMargin * 2));
Graphics objGraphics = Graphics.FromImage(objBitmap);

objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);

for (int i = 0; i < intSourceLength; i++)
{
//非法字元校正
if (AlphaBet.IndexOf(sourceCode[i]) == -1 || 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 = 0; i < intEncodeLength; i++) //繪製 Code39 barcode
{
intBarWidth = strEncode[i] == '1' ? thickLength : narrowLength;
objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
leftMargin, topMargin, intBarWidth, barCodeHeight);
leftMargin += intBarWidth;
}

//繪製 明碼
SizeF sizeF = objGraphics.MeasureString(sourceCode, font);
float x=(objBitmap.Width - sizeF.Width) / 2;
float y = objBitmap.Height - sizeF.Height;
objGraphics.FillRectangle(Brushes.White, x, y, sizeF.Width, sizeF.Height);
objGraphics.DrawString(sourceCode, font, Brushes.Black, x, y);

return objBitmap;
}

建立一個Winform程式拖一個PictureBox控制項,然後把方法返回的圖片綁到PictureBox上,就可以看到結果了。

以上代碼需要引用 System.Drawing 命名空間。

順帶提一句,還有一種最簡單的辦法就是下載條碼字型,然後輸出的時候設定字型為條碼字型,顯示的就是條碼了。不過你不能要求每個客戶機都裝這種字型。這個是個明顯的缺陷,所以不推薦使用這種方法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.