티스토리 뷰

서버에 바코드 전용 폰트를 설치해서 아래를 이용하여 만들었다가 그냥 나중에는 크리스탈리포트로 대체했다는...

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
}
}

'웹개발이야기 > ASP.NET' 카테고리의 다른 글

WebMatrix와 Razor 알아보기  (0) 2011.01.04
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함