C#條碼生成及打印實例代碼
本文實例為大家分享了C#條碼生成及打印的方法,供大家參考,具體內(nèi)容如下
string BarcodeString = "13043404455";//條碼
int ImgWidth = 520;
int ImgHeight = 120;
//打印按鈕
private void button1_Click(object sender, EventArgs e)
{
//實例化打印對象
PrintDocument printDocument1 = new PrintDocument();
//設(shè)置打印用的紙張,可以自定義紙張的大小(單位:mm). 當(dāng)打印高度不確定時也可以不設(shè)置
//printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 585, 800);
//注冊PrintPage事件,打印每一頁時會觸發(fā)該事件
printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
//開始打印
printDocument1.Print();
//打印預(yù)覽
//PrintPreviewDialog ppd = new PrintPreviewDialog();
//ppd.Document = printDocument1;
//ppd.ShowDialog();
}
//打印事件
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("\r\n\r\n\r\n");
sb.Append("*******興隆超市*******\r\n");
sb.Append("品名-----數(shù)量-----價格\r\n");
sb.Append("精品白沙 1 8元\r\n");
sb.Append("張新發(fā)檳榔 1 10元\r\n");
sb.Append("合計: 2 18元\r\n");
sb.Append("---收銀員:張三---\r\n");
sb.Append("---技術(shù)支持:李四---\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
DrawPrint(e, sb.ToString(), BarcodeString, ImgWidth, ImgHeight);
}
/// <summary>
/// 繪制打印內(nèi)容
/// </summary>
/// <param name="e">PrintPageEventArgs</param>
/// <param name="PrintStr">需要打印的文本</param>
/// <param name="BarcodeStr">條碼</param>
public void DrawPrint(PrintPageEventArgs e, string PrintStr, string BarcodeStr, int BarcodeWidth, int BarcodeHeight)
{
try
{
//繪制打印字符串
e.Graphics.DrawString(PrintStr, new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 1, 1);
if (!string.IsNullOrEmpty(BarcodeStr))
{
int PrintWidth = 175;
int PrintHeight = 35;
//繪制打印圖片
e.Graphics.DrawImage(CreateBarcodePicture(BarcodeStr, BarcodeWidth, BarcodeHeight), 0, 0, PrintWidth, PrintHeight);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 根據(jù)字符串生成條碼圖片( 需添加引用:BarcodeLib.dll )
/// </summary>
/// <param name="BarcodeString">條碼字符串</param>
/// <param name="ImgWidth">圖片寬帶</param>
/// <param name="ImgHeight">圖片高度</param>
/// <returns></returns>
public System.Drawing.Image CreateBarcodePicture(string BarcodeString, int ImgWidth, int ImgHeight)
{
BarcodeLib.Barcode b = new BarcodeLib.Barcode();//實例化一個條碼對象
BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;//編碼類型
//獲取條碼圖片
System.Drawing.Image BarcodePicture = b.Encode(type, BarcodeString, System.Drawing.Color.Black, System.Drawing.Color.White, ImgWidth, ImgHeight);
//BarcodePicture.Save(@"D:\Barcode.jpg");
b.Dispose();
return BarcodePicture;
}
//預(yù)覽條碼
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = CreateBarcodePicture(BarcodeString, ImgWidth, ImgHeight);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方
- 01-10C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及密碼的方法
- 01-10C#路徑,文件,目錄及IO常見操作匯總
- 01-10C#禁止textbox復(fù)制、粘貼、剪切及鼠標(biāo)右鍵的方法
- 01-10C#及WPF獲取本機所有字體和顏色的方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10C#中DataGridView動態(tài)添加行及添加列的方法
- 01-10winform實現(xiàn)限制及解除鼠標(biāo)移動范圍的方法
- 01-10C#實現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- 01-10基于C#實現(xiàn)簡單離線注冊碼生成與驗證


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?


