電腦上安裝了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,空格。