C#條碼產生及列印執行個體代碼_C#教程

來源:互聯網
上載者:User

本文執行個體為大家分享了C#條碼產生及列印的方法,供大家參考,具體內容如下

string BarcodeString = "13043404455";//條碼    int ImgWidth = 520;    int ImgHeight = 120;    //列印按鈕    private void button1_Click(object sender, EventArgs e)    {      //執行個體化列印對象      PrintDocument printDocument1 = new PrintDocument();      //設定列印用的紙張,可以自訂紙張的大小(單位:mm).   當列印高度不確定時也可以不設定      //printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 585, 800);      //註冊PrintPage事件,列印每一頁時會觸發該事件      printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);      //開始列印      printDocument1.Print();      //預覽列印      //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("品名-----數量-----價格\r\n");      sb.Append("精品白沙  1    8元\r\n");      sb.Append("張新發檳榔 1   10元\r\n");      sb.Append("合計:   2   18元\r\n");      sb.Append("---收銀員:張三---\r\n");      sb.Append("---支援人員:李四---\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>    /// 繪製列印內容    /// </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>    /// 根據字串產生條碼圖片( 需添加引用: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;    }    //預覽條碼    private void button2_Click(object sender, EventArgs e)    {      pictureBox1.Image = CreateBarcodePicture(BarcodeString, ImgWidth, ImgHeight);    }

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.