2008. 1. 21.

Create 39 Barcode using asp.net

어디선가 텍스트를 입력하면 바코드로 변환해주는 사이트가 있었는데... 기억안남..

암튼... 예전 내부에서 재고관리를 위해 바코드 출력을 해야할 일이 있어서 짜놨던 코드. 단 39 코드만 됨.


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Drawing.Drawing2D;

namespace barcode.test
{
///


/// img에 대한 요약 설명입니다.
///

public class BarCode : System.Web.UI.Page
{
private string m_BarCode;
int[] barCodeTable = {1, 0, 1, 0, 0, 2, 0, 2, 0, 1, 2, 0, 1, 0, 0, 1, 0, 1, 0, 2, 1, 0, 2, 0, 0, 1, 0, 1, 0, 2,
2, 0, 2, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 2, 0, 1, 0, 2, 2, 0, 1, 0, 0, 2, 0, 1, 0, 1,
1, 0, 2, 0, 0, 2, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 2, 0, 2, 2, 0, 1, 0, 0, 1, 0, 2, 0, 1,
1, 0, 2, 0, 0, 1, 0, 2, 0, 1, 2, 0, 1, 0, 1, 0, 0, 1, 0, 2, 1, 0, 2, 0, 1, 0, 0, 1, 0, 2,
2, 0, 2, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 2, 0, 0, 1, 0, 2, 2, 0, 1, 0, 2, 0, 0, 1, 0, 1,
1, 0, 2, 0, 2, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 2, 0, 2, 2, 0, 1, 0, 1, 0, 0, 2, 0, 1,
1, 0, 2, 0, 1, 0, 0, 2, 0, 1, 1, 0, 1, 0, 2, 0, 0, 2, 0, 1, 2, 0, 1, 0, 1, 0, 1, 0, 0, 2,
1, 0, 2, 0, 1, 0, 1, 0, 0, 2, 2, 0, 2, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 2, 0, 1, 0, 0, 2,
2, 0, 1, 0, 2, 0, 1, 0, 0, 1, 1, 0, 2, 0, 2, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 2, 0, 0, 2,
2, 0, 1, 0, 1, 0, 2, 0, 0, 1, 1, 0, 2, 0, 1, 0, 2, 0, 0, 1, 1, 0, 1, 0, 2, 0, 2, 0, 0, 1,
2, 0, 0, 1, 0, 1, 0, 1, 0, 2, 1, 0, 0, 2, 0, 1, 0, 1, 0, 2, 2, 0, 0, 2, 0, 1, 0, 1, 0, 1,
1, 0, 0, 1, 0, 2, 0, 1, 0, 2, 2, 0, 0, 1, 0, 2, 0, 1, 0, 1, 1, 0, 0, 2, 0, 2, 0, 1, 0, 1,
1, 0, 0, 1, 0, 1, 0, 2, 0, 2, 2, 0, 0, 1, 0, 1, 0, 2, 0, 1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 1,
1, 0, 0, 1, 0, 2, 0, 2, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1,
1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1};

double moduleWidth = 1;
int barHeight = 30;
int totalHeight = 45;
private void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType = "image/jpeg";

this.m_BarCode = Request.QueryString[0].ToString();

string barCode39 = "*" + m_BarCode + "*";

int pLeft = 1 ;

char[] barcodeChar = barCode39.ToCharArray();

for(int i= 0 ; i < barcodeChar.Length; i++)
{
if(getBarCodeTableIdx(barcodeChar[i]) >= 0)
{
pLeft = Convert.ToUInt16(getBarSize(pLeft,getBarCodeTableIdx(barcodeChar[i]),getBarLength(barcodeChar[i])));
}
}


Bitmap bitmap = new Bitmap(Convert.ToUInt16(pLeft) + 2,totalHeight);
Graphics graphic = Graphics.FromImage(bitmap);

Brush brush = new SolidBrush(Color.White);
graphic.FillRectangle(brush,0,0,Convert.ToUInt16(pLeft) + 2,totalHeight);
graphic.PageUnit = GraphicsUnit.Display;


pLeft = 1;

for(int i= 0 ; i < barcodeChar.Length; i++)
{
if(getBarCodeTableIdx(barcodeChar[i]) >= 0)
{
pLeft = Convert.ToUInt16(DrawBar(graphic,pLeft,getBarCodeTableIdx(barcodeChar[i]),getBarLength(barcodeChar[i])));
}
}

this.DrawString(graphic,this.m_BarCode);

bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);

bitmap.Dispose();
}
private void DrawString(Graphics pGraphics,string pBarCode)
{
if(pBarCode.Substring(0,1).Equals("P"))
{
Font ft = new Font("Courier",9f);
Brush brush = new SolidBrush(Color.Black);

pGraphics.DrawString(pBarCode.Substring(0,1),ft,brush,10f,0f);

Font ftu = new Font("Courier",9f,FontStyle.Underline);

pGraphics.DrawString(pBarCode.Substring(1,8),ftu,brush,23f,0f);

pGraphics.DrawString(pBarCode.Substring(9,5),ft,brush,90f,0f);

}
else
{
Font ft = new Font("Courier",9f);
Brush brush = new SolidBrush(Color.Black);

pGraphics.DrawString(pBarCode,ft,brush,10f,0f);
}
}
private double getBarSize(int pLeft,int pTableIdx,int pBarLength)
{
double tWidth = 0;

for(int i=0 ; i <= pBarLength ; i++)
{
tWidth = barCodeTable[pTableIdx + i] * moduleWidth;

if(barCodeTable[pTableIdx+i] !=0)
{
pLeft= pLeft + Convert.ToUInt16(tWidth);
}
else
{
pLeft= pLeft + Convert.ToInt16(moduleWidth);
}
}
return pLeft + moduleWidth * 1.8;
}
private double DrawBar(Graphics pGraphics,int pLeft,int pTableIdx,int pBarLength)
{
double tWidth = 0;

for(int i=0 ; i <= pBarLength ; i++)
{
tWidth = barCodeTable[pTableIdx + i] * moduleWidth;

if(barCodeTable[pTableIdx+i] !=0)
{
Brush brush = new SolidBrush(Color.Black);

pGraphics.FillRectangle(brush,pLeft, this.totalHeight - this.barHeight ,Convert.ToUInt16(tWidth), barHeight);
pLeft= pLeft + Convert.ToUInt16(tWidth);
}
else
{
pLeft= pLeft + Convert.ToInt16(moduleWidth);
}
}
return pLeft + moduleWidth * 1.8;
}
private int getBarCodeTableIdx(char pChar)
{
if(pChar >= '0' && pChar <='9')
return Convert.ToInt16(pChar.ToString()) * 10;
else if(pChar >= 'A' && pChar <= 'Z')
return (pChar -65 + 10) * 10;
else if(pChar >= 'a' && pChar <= 'z')
return (pChar - 97 + 10) * 10;
else if(pChar == '-')
return 36 * 10;
else if(pChar == '.')
return 37 * 10;
else if(pChar == ' ')
return 38 * 10;
else if(pChar == '*')
return 39 * 10;
else if(pChar == '$')
return 40 * 10;
else if(pChar == '/')
return 40 * 10 + 12 * 1;
else if(pChar == '+')
return 39 * 10 + 12 *2;
else if(pChar == '%')
return 39 * 10 + 12 * 3;
else
return -1;
}
private int getBarLength(char pChar)
{
if(pChar == '$'pChar == '/' pChar == '+' pChar == '%')
return 12;
else
return 9;
}
#region Web Form 디자이너에서 생성한 코드
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 이 호출은 ASP.NET Web Form 디자이너에 필요합니다.
//
InitializeComponent();
base.OnInit(e);
}

///
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

댓글 없음: