winfrom生成图片 打印一体

来源:互联网 发布:win7四核补丁优化 编辑:程序博客网 时间:2024/06/10 00:12

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.Drawing.Printing;
using System.IO;

namespace gong
{
    public partial class Form1 : Form
    {
        private System.Drawing.Printing.PrintDocument printDocument1;
        private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
        private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
        private System.Windows.Forms.PrintDialog printDialog1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button tbDraw;
        private PageSettings pageSet;
        ///   <summary>
        ///   必需的设计器变量。
        ///   </summary>
        private System.ComponentModel.Container components = null;
        public Form1()
        {
            InitializeComponent();
        }
        public enum Code39Model
        {
            /// <summary>
            /// 基本类别 1234567890ABC
            /// </summary>
            Code39Normal,
            /// <summary>
            /// 全ASCII方式 +A+B 来表示小写
            /// </summary>
            Code39FullAscII
        }
        /// <summary>
        /// Code39编码
        /// zgke@sina.com
        /// qq:116149
        /// </summary>
        public class Code39
        {
            private Hashtable m_Code39 = new Hashtable();

            private byte m_Magnify = 0;
            /// <summary>
            /// 放大倍数
            /// </summary>
            public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }

            private int m_Height = 40;
            /// <summary>
            /// 图形高
            /// </summary>
            public int Height { get { return m_Height; } set { m_Height = value; } }

            private Font m_ViewFont = null;
            /// <summary>
            /// 字体大小
            /// </summary>
            public Font ViewFont { get { return m_ViewFont; } set { m_ViewFont = value; } }

 

            public Code39()
            {

                m_Code39.Add("A", "1101010010110");
                m_Code39.Add("B", "1011010010110");
                m_Code39.Add("C", "1101101001010");
                m_Code39.Add("D", "1010110010110");
                m_Code39.Add("E", "1101011001010");
                m_Code39.Add("F", "1011011001010");
                m_Code39.Add("G", "1010100110110");
                m_Code39.Add("H", "1101010011010");
                m_Code39.Add("I", "1011010011010");
                m_Code39.Add("J", "1010110011010");
                m_Code39.Add("K", "1101010100110");
                m_Code39.Add("L", "1011010100110");
                m_Code39.Add("M", "1101101010010");
                m_Code39.Add("N", "1010110100110");
                m_Code39.Add("O", "1101011010010");
                m_Code39.Add("P", "1011011010010");
                m_Code39.Add("Q", "1010101100110");
                m_Code39.Add("R", "1101010110010");
                m_Code39.Add("S", "1011010110010");
                m_Code39.Add("T", "1010110110010");
                m_Code39.Add("U", "1100101010110");
                m_Code39.Add("V", "1001101010110");
                m_Code39.Add("W", "1100110101010");
                m_Code39.Add("X", "1001011010110");
                m_Code39.Add("Y", "1100101101010");
                m_Code39.Add("Z", "1001101101010");
                m_Code39.Add("0", "1010011011010");
                m_Code39.Add("1", "1101001010110");
                m_Code39.Add("2", "1011001010110");
                m_Code39.Add("3", "1101100101010");
                m_Code39.Add("4", "1010011010110");
                m_Code39.Add("5", "1101001101010");
                m_Code39.Add("6", "1011001101010");
                m_Code39.Add("7", "1010010110110");
                m_Code39.Add("8", "1101001011010");
                m_Code39.Add("9", "1011001011010");
                m_Code39.Add("+", "1001010010010");
                m_Code39.Add("-", "1001010110110");
                m_Code39.Add("*", "1001011011010");
                m_Code39.Add("/", "1001001010010");
                m_Code39.Add("%", "1010010010010");
                m_Code39.Add("$", "1001001001010");
                m_Code39.Add(".", "1100101011010");
                m_Code39.Add(" ", "1001101011010");

            }


            /// <summary>
            /// 获得条码图形
            /// </summary>
            /// <param name="p_Text">文字信息</param>
            /// <param name="p_Model">类别</param>
            /// <param name="p_StarChar">是否增加前后*号</param>
            /// <returns>图形</returns>
            public Bitmap GetCodeImage(string p_Text, Code39Model p_Model, bool p_StarChar)
            {
                string _ValueText = "";
                string _CodeText = "";
                char[] _ValueChar = null;
                switch (p_Model)
                {
                    case Code39Model.Code39Normal:
                        _ValueText = p_Text.ToUpper();
                        break;
                    default:
                        _ValueChar = p_Text.ToCharArray();
                        for (int i = 0; i != _ValueChar.Length; i++)
                        {
                            if ((int)_ValueChar[i] >= 97 && (int)_ValueChar[i] <= 122)
                            {
                                _ValueText += "+" + _ValueChar[i].ToString().ToUpper();

                            }
                            else
                            {
                                _ValueText += _ValueChar[i].ToString();
                            }
                        }
                        break;
                }


                _ValueChar = _ValueText.ToCharArray();

                if (p_StarChar == true) _CodeText += m_Code39["*"];

                for (int i = 0; i != _ValueChar.Length; i++)
                {
                    if (p_StarChar == true && _ValueChar[i] == '*') throw new Exception("带有起始符号不能出现*");

                    object _CharCode = m_Code39[_ValueChar[i].ToString()];
                    if (_CharCode == null) throw new Exception("不可用的字符" + _ValueChar[i].ToString());
                    _CodeText += _CharCode.ToString();
                }


                if (p_StarChar == true) _CodeText += m_Code39["*"];


                Bitmap _CodeBmp = GetImage(_CodeText);
                GetViewImage(_CodeBmp, p_Text);
                return _CodeBmp;
            }

 

            /// <summary>
            /// 绘制编码图形
            /// </summary>
            /// <param name="p_Text">编码</param>
            /// <returns>图形</returns>
            private Bitmap GetImage(string p_Text)
            {
                char[] _Value = p_Text.ToCharArray();


                //宽 == 需要绘制的数量*放大倍数 + 两个字的宽   
                Bitmap _CodeImage = new Bitmap(_Value.Length * ((int)m_Magnify + 1), (int)m_Height);
                Graphics _Garphics = Graphics.FromImage(_CodeImage);

                _Garphics.FillRectangle(Brushes.White, new Rectangle(0, 0, _CodeImage.Width, _CodeImage.Height));

                int _LenEx = 0;
                for (int i = 0; i != _Value.Length; i++)
                {
                    int _DrawWidth = m_Magnify + 1;
                    if (_Value[i] == '1')
                    {
                        _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _DrawWidth, m_Height));

                    }
                    else
                    {
                        _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _DrawWidth, m_Height));
                    }
                    _LenEx += _DrawWidth;
                }

 

                _Garphics.Dispose();
                return _CodeImage;
            }
            /// <summary>
            /// 绘制文字
            /// </summary>
            /// <param name="p_CodeImage">图形</param>
            /// <param name="p_Text">文字</param>
            private void GetViewImage(Bitmap p_CodeImage, string p_Text)
            {
                if (m_ViewFont == null) return;
                System.Drawing.Graphics _Graphics = Graphics.FromImage(p_CodeImage);
                SizeF _FontSize = _Graphics.MeasureString(p_Text, m_ViewFont);

                if (_FontSize.Width > p_CodeImage.Width || _FontSize.Height > p_CodeImage.Height - 20)
                {
                    _Graphics.Dispose();
                    return;
                }
                int _StarHeight = p_CodeImage.Height - (int)_FontSize.Height;

                _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarHeight, p_CodeImage.Width, (int)_FontSize.Height));

                int _StarWidth = (p_CodeImage.Width - (int)_FontSize.Width) / 2;

                _Graphics.DrawString(p_Text, m_ViewFont, Brushes.Black, _StarWidth, _StarHeight);

                _Graphics.Dispose();

            }
        }
     

        private void btn1_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn1.Text;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn2.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn3.Text;

        }

        private void btn4_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn4.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn5.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn6.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn7.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn8.Text;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn9.Text;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + this.btn0.Text;
        }

        private void btntui_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        ///   <summary>
        ///   清理所有正在使用的资源。
        ///   </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region   Windows   窗体设计器生成的代码
        ///   <summary>
        ///   设计器支持所需的方法   -   不要使用代码编辑器修改
        ///   此方法的内容。
        ///   </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.txtnumber = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btn8 = new System.Windows.Forms.Button();
            this.btntui = new System.Windows.Forms.Button();
            this.btn0 = new System.Windows.Forms.Button();
            this.btn6 = new System.Windows.Forms.Button();
            this.btn9 = new System.Windows.Forms.Button();
            this.btn5 = new System.Windows.Forms.Button();
            this.btn4 = new System.Windows.Forms.Button();
            this.btn7 = new System.Windows.Forms.Button();
            this.btn3 = new System.Windows.Forms.Button();
            this.btn2 = new System.Windows.Forms.Button();
            this.btn1 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
            this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
            this.printDialog1 = new System.Windows.Forms.PrintDialog();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.tbDraw = new System.Windows.Forms.Button();
            this.printButton = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.btnsearch = new System.Windows.Forms.Button();
            this.btnq = new System.Windows.Forms.Button();
            this.btnp = new System.Windows.Forms.Button();
            this.btno = new System.Windows.Forms.Button();
            this.btni = new System.Windows.Forms.Button();
            this.btnu = new System.Windows.Forms.Button();
            this.btny = new System.Windows.Forms.Button();
            this.btnt = new System.Windows.Forms.Button();
            this.btnr = new System.Windows.Forms.Button();
            this.btne = new System.Windows.Forms.Button();
            this.btnw = new System.Windows.Forms.Button();
            this.btna = new System.Windows.Forms.Button();
            this.btnd = new System.Windows.Forms.Button();
            this.btnl = new System.Windows.Forms.Button();
            this.btnk = new System.Windows.Forms.Button();
            this.btnj = new System.Windows.Forms.Button();
            this.btnh = new System.Windows.Forms.Button();
            this.btng = new System.Windows.Forms.Button();
            this.btnf = new System.Windows.Forms.Button();
            this.btns = new System.Windows.Forms.Button();
            this.btnx = new System.Windows.Forms.Button();
            this.btnc = new System.Windows.Forms.Button();
            this.btnv = new System.Windows.Forms.Button();
            this.btnb = new System.Windows.Forms.Button();
            this.btnn = new System.Windows.Forms.Button();
            this.btnm = new System.Windows.Forms.Button();
            this.btnz = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            //
            // txtnumber
            //
            this.txtnumber.Location = new System.Drawing.Point(211, 17);
            this.txtnumber.Name = "txtnumber";
            this.txtnumber.Size = new System.Drawing.Size(200, 21);
            this.txtnumber.TabIndex = 1;
            //
            // panel1
            //
            this.panel1.Controls.Add(this.btnx);
            this.panel1.Controls.Add(this.btnc);
            this.panel1.Controls.Add(this.btnv);
            this.panel1.Controls.Add(this.btnb);
            this.panel1.Controls.Add(this.btnn);
            this.panel1.Controls.Add(this.btnm);
            this.panel1.Controls.Add(this.btnz);
            this.panel1.Controls.Add(this.btns);
            this.panel1.Controls.Add(this.btnd);
            this.panel1.Controls.Add(this.btnf);
            this.panel1.Controls.Add(this.btng);
            this.panel1.Controls.Add(this.btnh);
            this.panel1.Controls.Add(this.btnj);
            this.panel1.Controls.Add(this.btnk);
            this.panel1.Controls.Add(this.btnl);
            this.panel1.Controls.Add(this.btna);
            this.panel1.Controls.Add(this.btnw);
            this.panel1.Controls.Add(this.btne);
            this.panel1.Controls.Add(this.btnr);
            this.panel1.Controls.Add(this.btnt);
            this.panel1.Controls.Add(this.btny);
            this.panel1.Controls.Add(this.btnu);
            this.panel1.Controls.Add(this.btni);
            this.panel1.Controls.Add(this.btno);
            this.panel1.Controls.Add(this.btnp);
            this.panel1.Controls.Add(this.btnq);
            this.panel1.Controls.Add(this.btn8);
            this.panel1.Controls.Add(this.btntui);
            this.panel1.Controls.Add(this.btn0);
            this.panel1.Controls.Add(this.btn6);
            this.panel1.Controls.Add(this.btn9);
            this.panel1.Controls.Add(this.btn5);
            this.panel1.Controls.Add(this.btn4);
            this.panel1.Controls.Add(this.btn7);
            this.panel1.Controls.Add(this.btn3);
            this.panel1.Controls.Add(this.btn2);
            this.panel1.Controls.Add(this.btn1);
            this.panel1.Location = new System.Drawing.Point(90, 41);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(467, 153);
            this.panel1.TabIndex = 2;
            //
            // btn8
            //
            this.btn8.Location = new System.Drawing.Point(282, 3);
            this.btn8.Name = "btn8";
            this.btn8.Size = new System.Drawing.Size(32, 23);
            this.btn8.TabIndex = 10;
            this.btn8.Text = "8";
            this.btn8.UseVisualStyleBackColor = true;
            this.btn8.Click += new System.EventHandler(this.btn8_Click);
            //
            // btntui
            //
            this.btntui.Location = new System.Drawing.Point(400, 4);
            this.btntui.Name = "btntui";
            this.btntui.Size = new System.Drawing.Size(48, 23);
            this.btntui.TabIndex = 9;
            this.btntui.Text = "重填";
            this.btntui.UseVisualStyleBackColor = true;
            this.btntui.Click += new System.EventHandler(this.btntui_Click);
            //
            // btn0
            //
            this.btn0.Location = new System.Drawing.Point(360, 3);
            this.btn0.Name = "btn0";
            this.btn0.Size = new System.Drawing.Size(32, 23);
            this.btn0.TabIndex = 8;
            this.btn0.Text = "0";
            this.btn0.UseVisualStyleBackColor = true;
            this.btn0.Click += new System.EventHandler(this.btn0_Click);
            //
            // btn6
            //
            this.btn6.Location = new System.Drawing.Point(207, 3);
            this.btn6.Name = "btn6";
            this.btn6.Size = new System.Drawing.Size(32, 23);
            this.btn6.TabIndex = 7;
            this.btn6.Text = "6";
            this.btn6.UseVisualStyleBackColor = true;
            this.btn6.Click += new System.EventHandler(this.btn6_Click);
            //
            // btn9
            //
            this.btn9.Location = new System.Drawing.Point(320, 3);
            this.btn9.Name = "btn9";
            this.btn9.Size = new System.Drawing.Size(32, 23);
            this.btn9.TabIndex = 6;
            this.btn9.Text = "9";
            this.btn9.UseVisualStyleBackColor = true;
            this.btn9.Click += new System.EventHandler(this.btn9_Click);
            //
            // btn5
            //
            this.btn5.Location = new System.Drawing.Point(166, 3);
            this.btn5.Name = "btn5";
            this.btn5.Size = new System.Drawing.Size(32, 23);
            this.btn5.TabIndex = 5;
            this.btn5.Text = "5";
            this.btn5.UseVisualStyleBackColor = true;
            this.btn5.Click += new System.EventHandler(this.btn5_Click);
            //
            // btn4
            //
            this.btn4.Location = new System.Drawing.Point(128, 4);
            this.btn4.Name = "btn4";
            this.btn4.Size = new System.Drawing.Size(32, 23);
            this.btn4.TabIndex = 4;
            this.btn4.Text = "4";
            this.btn4.UseVisualStyleBackColor = true;
            this.btn4.Click += new System.EventHandler(this.btn4_Click);
            //
            // btn7
            //
            this.btn7.Location = new System.Drawing.Point(245, 3);
            this.btn7.Name = "btn7";
            this.btn7.Size = new System.Drawing.Size(32, 23);
            this.btn7.TabIndex = 3;
            this.btn7.Text = "7";
            this.btn7.UseVisualStyleBackColor = true;
            this.btn7.Click += new System.EventHandler(this.btn7_Click);
            //
            // btn3
            //
            this.btn3.Location = new System.Drawing.Point(90, 4);
            this.btn3.Name = "btn3";
            this.btn3.Size = new System.Drawing.Size(32, 23);
            this.btn3.TabIndex = 2;
            this.btn3.Text = "3";
            this.btn3.UseVisualStyleBackColor = true;
            this.btn3.Click += new System.EventHandler(this.btn3_Click);
            //
            // btn2
            //
            this.btn2.Location = new System.Drawing.Point(47, 4);
            this.btn2.Name = "btn2";
            this.btn2.Size = new System.Drawing.Size(32, 23);
            this.btn2.TabIndex = 1;
            this.btn2.Text = "2";
            this.btn2.UseVisualStyleBackColor = true;
            this.btn2.Click += new System.EventHandler(this.btn2_Click);
            //
            // btn1
            //
            this.btn1.Location = new System.Drawing.Point(4, 4);
            this.btn1.Name = "btn1";
            this.btn1.Size = new System.Drawing.Size(32, 23);
            this.btn1.TabIndex = 0;
            this.btn1.Text = "1";
            this.btn1.UseVisualStyleBackColor = true;
            this.btn1.Click += new System.EventHandler(this.btn1_Click);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(133, 457);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(144, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "printDialog ";
            this.button1.Visible = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // printDocument1
            //
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
            //
            // printPreviewDialog1
            //
            this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
            this.printPreviewDialog1.Enabled = true;
            this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
            this.printPreviewDialog1.Name = "printPreviewDialog1 ";
            this.printPreviewDialog1.Visible = false;
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(288, 457);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(144, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "pageSetupDialog ";
            this.button2.Visible = false;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(438, 457);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(144, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "printPreviewDialog ";
            this.button3.Visible = false;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // tbDraw
            //
            this.tbDraw.Location = new System.Drawing.Point(50, 457);
            this.tbDraw.Name = "tbDraw";
            this.tbDraw.Size = new System.Drawing.Size(75, 23);
            this.tbDraw.TabIndex = 3;
            this.tbDraw.Text = "Draw ";
            this.tbDraw.Visible = false;
            this.tbDraw.Click += new System.EventHandler(this.tbDraw_Click);
            //
            // printButton
            //
            this.printButton.Location = new System.Drawing.Point(336, 221);
            this.printButton.Name = "printButton";
            this.printButton.Size = new System.Drawing.Size(75, 23);
            this.printButton.TabIndex = 4;
            this.printButton.Text = "打印";
            this.printButton.UseVisualStyleBackColor = true;
            this.printButton.Click += new System.EventHandler(this.printButton_Click);
            //
            // pictureBox1
            //
            this.pictureBox1.Location = new System.Drawing.Point(37, 351);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(144, 50);
            this.pictureBox1.TabIndex = 5;
            this.pictureBox1.TabStop = false;
            //
            // btnsearch
            //
            this.btnsearch.Location = new System.Drawing.Point(193, 221);
            this.btnsearch.Name = "btnsearch";
            this.btnsearch.Size = new System.Drawing.Size(75, 23);
            this.btnsearch.TabIndex = 6;
            this.btnsearch.Text = "搜索";
            this.btnsearch.UseVisualStyleBackColor = true;
            this.btnsearch.Click += new System.EventHandler(this.btnsearch_Click);
            //
            // btnq
            //
            this.btnq.Location = new System.Drawing.Point(22, 33);
            this.btnq.Name = "btnq";
            this.btnq.Size = new System.Drawing.Size(32, 23);
            this.btnq.TabIndex = 11;
            this.btnq.Text = "q";
            this.btnq.UseVisualStyleBackColor = true;
            this.btnq.Click += new System.EventHandler(this.btnq_Click);
            //
            // btnp
            //
            this.btnp.Location = new System.Drawing.Point(384, 33);
            this.btnp.Name = "btnp";
            this.btnp.Size = new System.Drawing.Size(32, 23);
            this.btnp.TabIndex = 12;
            this.btnp.Text = "p";
            this.btnp.UseVisualStyleBackColor = true;
            this.btnp.Click += new System.EventHandler(this.btnp_Click);
            //
            // btno
            //
            this.btno.Location = new System.Drawing.Point(345, 33);
            this.btno.Name = "btno";
            this.btno.Size = new System.Drawing.Size(32, 23);
            this.btno.TabIndex = 13;
            this.btno.Text = "o";
            this.btno.UseVisualStyleBackColor = true;
            this.btno.Click += new System.EventHandler(this.btno_Click);
            //
            // btni
            //
            this.btni.Location = new System.Drawing.Point(306, 32);
            this.btni.Name = "btni";
            this.btni.Size = new System.Drawing.Size(32, 23);
            this.btni.TabIndex = 14;
            this.btni.Text = "i";
            this.btni.UseVisualStyleBackColor = true;
            this.btni.Click += new System.EventHandler(this.btni_Click);
            //
            // btnu
            //
            this.btnu.Location = new System.Drawing.Point(265, 33);
            this.btnu.Name = "btnu";
            this.btnu.Size = new System.Drawing.Size(32, 23);
            this.btnu.TabIndex = 15;
            this.btnu.Text = "u";
            this.btnu.UseVisualStyleBackColor = true;
            this.btnu.Click += new System.EventHandler(this.btnu_Click);
            //
            // btny
            //
            this.btny.Location = new System.Drawing.Point(225, 32);
            this.btny.Name = "btny";
            this.btny.Size = new System.Drawing.Size(32, 23);
            this.btny.TabIndex = 16;
            this.btny.Text = "y";
            this.btny.UseVisualStyleBackColor = true;
            this.btny.Click += new System.EventHandler(this.btny_Click);
            //
            // btnt
            //
            this.btnt.Location = new System.Drawing.Point(186, 33);
            this.btnt.Name = "btnt";
            this.btnt.Size = new System.Drawing.Size(32, 23);
            this.btnt.TabIndex = 17;
            this.btnt.Text = "t";
            this.btnt.UseVisualStyleBackColor = true;
            this.btnt.Click += new System.EventHandler(this.btnt_Click);
            //
            // btnr
            //
            this.btnr.Location = new System.Drawing.Point(141, 33);
            this.btnr.Name = "btnr";
            this.btnr.Size = new System.Drawing.Size(32, 23);
            this.btnr.TabIndex = 18;
            this.btnr.Text = "r";
            this.btnr.UseVisualStyleBackColor = true;
            this.btnr.Click += new System.EventHandler(this.btnr_Click);
            //
            // btne
            //
            this.btne.Location = new System.Drawing.Point(103, 33);
            this.btne.Name = "btne";
            this.btne.Size = new System.Drawing.Size(32, 23);
            this.btne.TabIndex = 19;
            this.btne.Text = "e";
            this.btne.UseVisualStyleBackColor = true;
            this.btne.Click += new System.EventHandler(this.btne_Click);
            //
            // btnw
            //
            this.btnw.Location = new System.Drawing.Point(65, 33);
            this.btnw.Name = "btnw";
            this.btnw.Size = new System.Drawing.Size(32, 23);
            this.btnw.TabIndex = 20;
            this.btnw.Text = "w";
            this.btnw.UseVisualStyleBackColor = true;
            this.btnw.Click += new System.EventHandler(this.btnw_Click);
            //
            // btna
            //
            this.btna.Location = new System.Drawing.Point(47, 62);
            this.btna.Name = "btna";
            this.btna.Size = new System.Drawing.Size(32, 23);
            this.btna.TabIndex = 21;
            this.btna.Text = "a";
            this.btna.UseVisualStyleBackColor = true;
            this.btna.Click += new System.EventHandler(this.btna_Click);
            //
            // btnd
            //
            this.btnd.Location = new System.Drawing.Point(128, 62);
            this.btnd.Name = "btnd";
            this.btnd.Size = new System.Drawing.Size(32, 23);
            this.btnd.TabIndex = 29;
            this.btnd.Text = "d";
            this.btnd.UseVisualStyleBackColor = true;
            this.btnd.Click += new System.EventHandler(this.btnd_Click);
            //
            // btnl
            //
            this.btnl.Location = new System.Drawing.Point(370, 62);
            this.btnl.Name = "btnl";
            this.btnl.Size = new System.Drawing.Size(32, 23);
            this.btnl.TabIndex = 23;
            this.btnl.Text = "l";
            this.btnl.UseVisualStyleBackColor = true;
            this.btnl.Click += new System.EventHandler(this.btnl_Click);
            //
            // btnk
            //
            this.btnk.Location = new System.Drawing.Point(331, 61);
            this.btnk.Name = "btnk";
            this.btnk.Size = new System.Drawing.Size(32, 23);
            this.btnk.TabIndex = 24;
            this.btnk.Text = "k";
            this.btnk.UseVisualStyleBackColor = true;
            this.btnk.Click += new System.EventHandler(this.btnk_Click);
            //
            // btnj
            //
            this.btnj.Location = new System.Drawing.Point(290, 62);
            this.btnj.Name = "btnj";
            this.btnj.Size = new System.Drawing.Size(32, 23);
            this.btnj.TabIndex = 25;
            this.btnj.Text = "j";
            this.btnj.UseVisualStyleBackColor = true;
            this.btnj.Click += new System.EventHandler(this.btnj_Click);
            //
            // btnh
            //
            this.btnh.Location = new System.Drawing.Point(250, 61);
            this.btnh.Name = "btnh";
            this.btnh.Size = new System.Drawing.Size(32, 23);
            this.btnh.TabIndex = 26;
            this.btnh.Text = "h";
            this.btnh.UseVisualStyleBackColor = true;
            this.btnh.Click += new System.EventHandler(this.btnh_Click);
            //
            // btng
            //
            this.btng.Location = new System.Drawing.Point(211, 62);
            this.btng.Name = "btng";
            this.btng.Size = new System.Drawing.Size(32, 23);
            this.btng.TabIndex = 27;
            this.btng.Text = "g";
            this.btng.UseVisualStyleBackColor = true;
            this.btng.Click += new System.EventHandler(this.btng_Click);
            //
            // btnf
            //
            this.btnf.Location = new System.Drawing.Point(166, 62);
            this.btnf.Name = "btnf";
            this.btnf.Size = new System.Drawing.Size(32, 23);
            this.btnf.TabIndex = 28;
            this.btnf.Text = "f";
            this.btnf.UseVisualStyleBackColor = true;
            this.btnf.Click += new System.EventHandler(this.btnf_Click);
            //
            // btns
            //
            this.btns.Location = new System.Drawing.Point(90, 62);
            this.btns.Name = "btns";
            this.btns.Size = new System.Drawing.Size(32, 23);
            this.btns.TabIndex = 30;
            this.btns.Text = "s";
            this.btns.UseVisualStyleBackColor = true;
            this.btns.Click += new System.EventHandler(this.btns_Click);
            //
            // btnx
            //
            this.btnx.Location = new System.Drawing.Point(108, 91);
            this.btnx.Name = "btnx";
            this.btnx.Size = new System.Drawing.Size(32, 23);
            this.btnx.TabIndex = 39;
            this.btnx.Text = "x";
            this.btnx.UseVisualStyleBackColor = true;
            this.btnx.Click += new System.EventHandler(this.btnx_Click);
            //
            // btnc
            //
            this.btnc.Location = new System.Drawing.Point(146, 91);
            this.btnc.Name = "btnc";
            this.btnc.Size = new System.Drawing.Size(32, 23);
            this.btnc.TabIndex = 38;
            this.btnc.Text = "c";
            this.btnc.UseVisualStyleBackColor = true;
            this.btnc.Click += new System.EventHandler(this.btnc_Click);
            //
            // btnv
            //
            this.btnv.Location = new System.Drawing.Point(184, 91);
            this.btnv.Name = "btnv";
            this.btnv.Size = new System.Drawing.Size(32, 23);
            this.btnv.TabIndex = 37;
            this.btnv.Text = "v";
            this.btnv.UseVisualStyleBackColor = true;
            this.btnv.Click += new System.EventHandler(this.btnv_Click);
            //
            // btnb
            //
            this.btnb.Location = new System.Drawing.Point(229, 91);
            this.btnb.Name = "btnb";
            this.btnb.Size = new System.Drawing.Size(32, 23);
            this.btnb.TabIndex = 36;
            this.btnb.Text = "b";
            this.btnb.UseVisualStyleBackColor = true;
            this.btnb.Click += new System.EventHandler(this.btnb_Click);
            //
            // btnn
            //
            this.btnn.Location = new System.Drawing.Point(268, 90);
            this.btnn.Name = "btnn";
            this.btnn.Size = new System.Drawing.Size(32, 23);
            this.btnn.TabIndex = 35;
            this.btnn.Text = "n";
            this.btnn.UseVisualStyleBackColor = true;
            this.btnn.Click += new System.EventHandler(this.btnn_Click);
            //
            // btnm
            //
            this.btnm.Location = new System.Drawing.Point(308, 91);
            this.btnm.Name = "btnm";
            this.btnm.Size = new System.Drawing.Size(32, 23);
            this.btnm.TabIndex = 34;
            this.btnm.Text = "m";
            this.btnm.UseVisualStyleBackColor = true;
            this.btnm.Click += new System.EventHandler(this.btnm_Click);
            //
            // btnz
            //
            this.btnz.Location = new System.Drawing.Point(65, 91);
            this.btnz.Name = "btnz";
            this.btnz.Size = new System.Drawing.Size(32, 23);
            this.btnz.TabIndex = 31;
            this.btnz.Text = "z";
            this.btnz.UseVisualStyleBackColor = true;
            this.btnz.Click += new System.EventHandler(this.btnz_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(127, 23);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 12);
            this.label1.TabIndex = 7;
            this.label1.Text = "请输入条形码";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(709, 516);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnsearch);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.printButton);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.txtnumber);
            this.Controls.Add(this.tbDraw);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1 ";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// 打印的内容
        /// </summary>
        /// <param name="g"></param>
        private void PaintDocument(Graphics g)
        {
            string number = this.txtnumber.Text;
            if (number == "")
            {
                MessageBox.Show("请输入条码编号");
            }
            else
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=db1.mdb";
                //strConnection += HttpContext.Current.Server.MapPath("db1.mdb");
                OleDbConnection conn = new OleDbConnection(strConnection);
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("select id,ename,sex,egadress,egname,egurl,eprovice,ecity,ebranch,ezip,ephone," +
                                                     "emobile,efax,eemail,etname,etemail,etext,eeid,eatest,ebtest,ectest,edtest " +
                                                     " from ExhUser where ebtest like '%" + number + "%'", conn);
                OleDbDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {


                    string id = dr["id"].ToString();
                    string ename = dr["ename"].ToString();
                    string sex = dr["sex"].ToString();
                    string egadress = dr["egadress"].ToString();
                    string egname = dr["egname"].ToString();
                    string egurl = dr["egurl"].ToString();
                    string eprovice = dr["eprovice"].ToString();
                    string ecity = dr["ecity"].ToString();
                    string ebranch = dr["ebranch"].ToString();
                    string ezip = dr["ezip"].ToString();
                    string ephone = dr["ephone"].ToString();
                    string emobile = dr["emobile"].ToString();
                    string efax = dr["efax"].ToString();
                    string eemail = dr["eemail"].ToString();
                    string etname = dr["etname"].ToString();
                    string etemail = dr["etemail"].ToString();
                    string etext = dr["etext"].ToString();
                    string eeid = dr["eeid"].ToString();
                    string eatest = dr["eatest"].ToString();
                    string ebtest = dr["ebtest"].ToString();
                    string ectest = dr["ectest"].ToString();
                    string edtest = dr["edtest"].ToString();
               
                    //Code39 code39 = new Code39();
                  //code39.Height = 120;
                   // code39.Magnify = 1;
                   // code39.ViewFont = new Font("宋体", 20);
                    //code39.GetCodeImage(number, Code39Model.Code39Normal, true).Save("bmpimages/" + number + ".jpg");
                    //code39.GetCodeImage(number, Code39Model.Code39Normal, true).Save("~/bmpimages/" + number + ".jpg");
                    //this.pictureBox1.ImageLocation = "bumimages/" + number + ".jpg";
                    //////////////////////////////////////////////////////////////////////////////////////////
                    // p.ImageLocation = "bmpimages/1234.jpg";
                    string url = "bmpimages/"+number+".jpg";
                    Image img = Image.FromFile(url);
                    this.pictureBox1.Image = img;
                    g.DrawImageUnscaled(pictureBox1.Image, 10,20);//打印的图片
                    g.DrawString(ename, this.Font, Brushes.Black, 10, 20);  
                }
                else
                {
                    MessageBox.Show("没有相关数据!");
                }
                conn.Close();
            }

 

 

            //g.DrawRectangle(Pens.Black, 200, 200, 200, 200);   //图形
            //g.DrawString("打印测试 ", this.Font, Brushes.Black, 100, 300);   //如果多行,StreamRead

        }

        private void tbDraw_Click(object sender, System.EventArgs e)
        {
            Graphics g = this.CreateGraphics();   //为当前控件创建画布
            PaintDocument(g);
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;   //先建立画布
            PaintDocument(g);
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                PrinterSettings printerSet = new PrinterSettings();   //打印机设置
                if (this.pageSet != null)
                {
                    printDocument1.DefaultPageSettings = this.pageSet;
                }
                printDialog1.PrinterSettings = printerSet;   //打印前设置打打印机

                printDialog1.Document = printDocument1;
                printDocument1.DocumentName = "test ";
                if (printDialog1.ShowDialog() == DialogResult.OK)
                {
                    printDocument1.Print();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (this.pageSet == null)
                    this.pageSet = new PageSettings();
                pageSetupDialog1.PageSettings = this.pageSet;   //必须设置pageSet
                //         pageSetupDialog1.Document   =   printDocument1;        
                pageSetupDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (this.pageSet != null)
                    printDocument1.DefaultPageSettings = this.pageSet;

                printPreviewDialog1.Document = printDocument1;
                printPreviewDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

 

        //如果要打印整个窗体
        [System.Runtime.InteropServices.DllImport("gdi32.dll ")]
        public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        private Bitmap memoryImage;
        private void CaptureScreen()
        {
            Graphics mygraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();
            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }


        /// <summary>
        /// 打印按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printButton_Click(System.Object sender, System.EventArgs e)
        {
            string number = this.txtnumber.Text;

            if (number == "")
            {
                MessageBox.Show("请输入条码编号");
            }
            else
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=db1.mdb";
                //strConnection += HttpContext.Current.Server.MapPath("db1.mdb");
                OleDbConnection conn = new OleDbConnection(strConnection);
                conn.Open();

                OleDbCommand cmd = new OleDbCommand("select id,ename,sex,egadress,egname,egurl,eprovice,ecity,ebranch,ezip,ephone," +
                                                     "emobile,efax,eemail,etname,etemail,etext,eeid,eatest,ebtest,ectest,edtest " +
                                                     " from ExhUser where ebtest like '%" + number + "%'", conn);
                OleDbDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {

                    Code39 code39 = new Code39();
                    code39.Height = 120;
                    code39.Magnify = 1;
                    code39.ViewFont = new Font("宋体", 20);
                    code39.GetCodeImage(number, Code39Model.Code39Normal, true).Save("bmpimages/" + number + ".jpg");
                    //code39.GetCodeImage("321654987", Code39Model.Code39Normal, true).Save("~/bmpimages/" + "321654987" + ".bmp");
                    //this.pictureBox1.ImageLocation = "bumimages/" + number + ".jpg";
                    ///打印的方法
                    CaptureScreen();
                    printDocument1.Print();
                }
                else
                {
                    MessageBox.Show("没有相关数据");
                }


                conn.Close();


            }
          
        }


        /// <summary>
        /// 搜索的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsearch_Click(object sender, EventArgs e)
        {
            string number = this.txtnumber.Text;

            if (number == "")
            {
                MessageBox.Show("请输入条码编号");
            }
            else
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=db1.mdb";
                //strConnection += HttpContext.Current.Server.MapPath("db1.mdb");
                OleDbConnection conn = new OleDbConnection(strConnection);
                conn.Open();

                OleDbCommand cmd = new OleDbCommand("select id,ename,sex,egadress,egname,egurl,eprovice,ecity,ebranch,ezip,ephone," +
                                                     "emobile,efax,eemail,etname,etemail,etext,eeid,eatest,ebtest,ectest,edtest " +
                                                     " from ExhUser where ebtest like '%" + number + "%'", conn);
                OleDbDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {

                    Code39 code39 = new Code39();
                    code39.Height = 120;
                    code39.Magnify = 1;
                    code39.ViewFont = new Font("宋体", 20);
                    //code39.GetCodeImage(number, Code39Model.Code39Normal, true).Save("bmpimages/" + number + ".jpg");
                    //code39.GetCodeImage("321654987", Code39Model.Code39Normal, true).Save("~/bmpimages/" + "321654987" + ".bmp");
                    //this.pictureBox1.ImageLocation = "bumimages/" + number + ".jpg";

                }
                else
                {
                    MessageBox.Show("没有相关数据");
                }


                conn.Close();


            }
        }

        private void btnq_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "q";
        }

        private void btnw_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "w";
        }

        private void btne_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "e";
        }

        private void btnr_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "r";
        }

        private void btnt_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "t";
        }

        private void btny_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "y";
        }

        private void btnu_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "u";
        }

        private void btni_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "i";
        }

        private void btno_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "o";
        }

        private void btnp_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "p";
        }

        private void btna_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "a";
        }

        private void btns_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "s";
        }

        private void btnd_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "d";
        }

        private void btnf_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "f";
        }

        private void btng_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "g";
        }

        private void btnh_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "h";
        }

        private void btnj_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "j";
        }

        private void btnk_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "k";
        }

        private void btnl_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "l";
        }

        private void btnz_Click(object sender, EventArgs e)
        {
             this.txtnumber.Text = this.txtnumber.Text + "z";
        }

        private void btnx_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "x";
        }

        private void btnc_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "c";
        }

        private void btnv_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "v";
        }

        private void btnb_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "b";
        }

        private void btnn_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "n";
        }

        private void btnm_Click(object sender, EventArgs e)
        {
            this.txtnumber.Text = this.txtnumber.Text + "m";
        }

    }
}

 

原创粉丝点击