www.pudn.com > testQR_DM.rar > Frm_Main.cs, change:2014-05-06,size:7353b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
 
using ThoughtWorks.QRCode.Codec; 
using ThoughtWorks.QRCode.Codec.Data; 
using ThoughtWorks.QRCode.Codec.Util; 
using DataMatrix.net; 
 
namespace testQR_DM 
{ 
    public partial class Frm_Main : Form 
    { 
        string sCode128_height = "50"; 
        string sBarcode = ""; 
        static public DotNetBarcode bc1; 
        public Frm_Main() 
        { 
            InitializeComponent(); 
        } 
 
        private void Frm_Main_Load(object sender, EventArgs e) 
        { 
            this.tb_Text.Text = "1234567890"; 
            this.tb_Text.Focus(); 
            bc1 = new DotNetBarcode(DotNetBarcode.Types.QRCode); 
        } 
        //显示code128码 
        private void ViewCode128Barcode() 
        { 
            EnCode128.Code128 _Code = new EnCode128.Code128(); 
            _Code.ValueFont = new Font("宋体", 20); 
            _Code.Height = Convert.ToUInt32(sCode128_height); 
            if (sBarcode.Length % 2 == 0) 
            { 
                System.Drawing.Bitmap imgTemp = _Code.GetCodeImage(sBarcode, EnCode128.Code128.Encode.Code128C); 
                this.pictureBox1.Image = imgTemp; 
            } 
            else 
            { 
                System.Drawing.Bitmap imgTemp = _Code.GetCodeImage(sBarcode, EnCode128.Code128.Encode.Code128A); 
                this.pictureBox1.Image = imgTemp; 
            } 
        } 
        //显示QR码 
        private void ViewQRBarcode() 
        { 
             
        } 
        //显示DataMatrix码 
        private void ViewDMBarcode() 
        { 
            byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(sBarcode); 
            byte[] bytes1 = Encoding.Convert(Encoding.GetEncoding("gb2312"), Encoding.UTF8, bytes); 
            sBarcode = Encoding.UTF8.GetString(bytes1); 
            System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
            DmtxImageEncoder encoder = new DmtxImageEncoder(); 
            DmtxImageEncoderOptions options = new DmtxImageEncoderOptions(); 
            //调整图片大小 
             
            options.ModuleSize = 5; 
            //这里需要修改 
            options.MarginSize = Convert.ToInt32(10*(10/Convert.ToDouble(sBarcode.Length))); 
            options.BackColor = System.Drawing.Color.White; 
            options.ForeColor = System.Drawing.Color.Green; 
            Bitmap encodedBitmap = encoder.EncodeImage(sBarcode, options.MarginSize, options.ModuleSize); 
            //encodedBitmap.Save(ms, ImageFormat.Gif); 
            this.pictureBox1.Image = encodedBitmap; 
        } 
        //生成条码 
        private void btn_Encode_Click(object sender, EventArgs e) 
        { 
            sBarcode = this.tb_Text.Text.Trim().ToString(); 
            if (sBarcode == "") 
            { 
                MessageBox.Show("请先输入字符!"); 
                this.tb_Text.Focus(); 
            } 
            else 
            { 
                if (this.radioButton1.Checked == true) 
                { 
                    this.pictureBox1.Image = null; 
                    ViewCode128Barcode(); 
                } 
                if (this.radioButton2.Checked == true) 
                { 
                    this.pictureBox1.Refresh(); 
                } 
                if (this.radioButton3.Checked == true) 
                { 
                    this.pictureBox1.Image = null; 
                    ViewDMBarcode(); 
                } 
            } 
        } 
        //绘制 
        private void pictureBox1_Paint(object sender, PaintEventArgs e) 
        { 
            if (this.radioButton2.Checked == true) 
            { 
                Single x1; 
                Single y1; 
                Single x2; 
                Single y2; 
                x1 = 0; 
                y1 = 0; 
                x2 = this.pictureBox1.Size.Width; 
                y2 = this.pictureBox1.Size.Height; 
                bc1.Type = DotNetBarcode.Types.QRCode; 
 
                bc1.WriteBar(sBarcode, x1, y1, x2, y2, e.Graphics); 
            } 
        } 
        //清空 
        private void btn_Clear_Click(object sender, EventArgs e) 
        { 
            this.tb_Text.Text = ""; 
        } 
        //另存为图片 
        private void btn_Save_Click(object sender, EventArgs e) 
        { 
            saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png"; 
            saveFileDialog1.Title = "Save"; 
            saveFileDialog1.FileName = string.Empty; 
            saveFileDialog1.ShowDialog(); 
 
            // If the file name is not an empty string open it for saving. 
            if (saveFileDialog1.FileName != "") 
            { 
                // Saves the Image via a FileStream created by the OpenFile method. 
                System.IO.FileStream fs = 
                   (System.IO.FileStream)saveFileDialog1.OpenFile(); 
                // Saves the Image in the appropriate ImageFormat based upon the 
                // File type selected in the dialog box. 
                // NOTE that the FilterIndex property is one-based. 
                switch (saveFileDialog1.FilterIndex) 
                { 
                    case 1: 
                        this.pictureBox1.Image.Save(fs, 
                           System.Drawing.Imaging.ImageFormat.Jpeg); 
                        break; 
 
                    case 2: 
                        this.pictureBox1.Image.Save(fs, 
                           System.Drawing.Imaging.ImageFormat.Bmp); 
                        break; 
 
                    case 3: 
                        this.pictureBox1.Image.Save(fs, 
                           System.Drawing.Imaging.ImageFormat.Gif); 
                        break; 
                    case 4: 
                        this.pictureBox1.Image.Save(fs, 
                           System.Drawing.Imaging.ImageFormat.Png); 
                        break; 
                } 
                fs.Close(); 
            } 
        } 
        //打开图片 
        private void btn_Open_Click(object sender, EventArgs e) 
        { 
            openFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png|All files (*.*)|*.*"; 
            openFileDialog1.FilterIndex = 1; 
            openFileDialog1.RestoreDirectory = true; 
            openFileDialog1.FileName = string.Empty; 
 
            if (openFileDialog1.ShowDialog() == DialogResult.OK) 
            { 
                String fileName = openFileDialog1.FileName; 
                this.pictureBox2.Image = new Bitmap(fileName); 
            } 
        } 
        //解码 
        private void btn_Decode_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                QRCodeDecoder decoder = new QRCodeDecoder(); 
                //QRCodeDecoder.Canvas = new ConsoleCanvas(); 
                String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(this.pictureBox2.Image))); 
                this.tb_Decode.Text = decodedString; 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        } 
    } 
}