C#(Winform)實現條碼列印

來源:互聯網
上載者:User

電腦上安裝了39條碼字型:C39HrP24DlTt

先看下控制項介面

轉換條碼字型

        private void button1_Click(object sender, EventArgs e)
{
Bitmap b1 = new Bitmap(Convert.ToInt32(textBox2.Text), Convert.ToInt32 ( textBox3.Text));
Graphics g1 = Graphics.FromImage(b1);
Font font1 = new Font("C39HrP24DlTt", Convert.ToInt32(textBox4.Text));
g1.DrawString(textBox1.Text.ToString(), font1, Brushes.Black, new PointF(Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text)));
pictureBox1.BackgroundImage = b1;
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
}

pageSetupDialog1、printDialog1、printPreviewDialog1的document屬性都選擇為printDocument1

給printDocument1添加一個PrintPage事件printDocument1_PrintPage

列印設定

        private void button2_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog();

}
預覽列印

        private void button3_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.ShowDialog();

}
列印

        private void button4_Click(object sender, EventArgs e)
{
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
}
列印內容的設定

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//列印內容 為 整個Form
//Image myFormImage;
//myFormImage = new Bitmap(this.Width, this.Height);
//Graphics g = Graphics.FromImage(myFormImage);
//g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
//e.Graphics.DrawImage(myFormImage, 0, 0);

//列印內容 為 局部的 this.groupBox1
Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

//列印內容 為 自訂常值內容
/*Font font = new Font("宋體", 12);
Brush bru = Brushes.Blue;
for (int i = 1; i <= 5; i++)
{
e.Graphics.DrawString("Hello world ", font, bru, i*20, i*20);
}*/
}
public Form1()

        public Form1()
{
InitializeComponent();
this.printDocument1.OriginAtMargins = true;//啟用頁面邊界
this.pageSetupDialog1.EnableMetric = true; //以毫米為單位
}

沒有做異常處理,比如沒有啟動列印服務的情況下,程式會不友好地報錯。

轉成39條碼字型,支援的字元有
A-Z,26個字母,不分大小寫,都表示成大寫;
0-9,10個數字;
+-*/%$.,7個符號;
space,空格。

相關文章

聯繫我們

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