using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;
using MAKEPDF417Lib;


namespace PDF417_DLL_Demo
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            //CoInitialize(NULL);
            InitializeComponent();
            textBox1.Text="5";
            textBox2.Text="3";
            textBox3.Text = "3";
            textBox4.Text="24";
            textBox5.Text="38";
            textBox6.Text="2004062100000014ST0005机电式";
            textBox7.Text = "9";
            textBox8.Text = "12345678";
            textBox9.Text = "0";
            textBox10.Text = "40";
            textBox11.Text = "10";
            textBox12.Text = "5";
            barcode = new BarCodePDF417Class();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string content = textBox6.Text;
            int nClumn = Int32.Parse(textBox1.Text);
            int nErr = Int32.Parse(textBox2.Text);
            int nHLRatio = Int32.Parse(textBox3.Text);
            int nHeight = Int32.Parse(textBox5.Text);
            int nColor = Int32.Parse(textBox4.Text);
            string szText = textBox8.Text;
            int lFontSize = Int32.Parse(textBox9.Text);
            int lTxtHeight = Int32.Parse(textBox10.Text);
            int lHmargin = Int32.Parse(textBox11.Text);
            int lVmargin = Int32.Parse(textBox12.Text);
            int lTxtCntOneLine = Int32.Parse(textBox7.Text);
            byte[] b = System.Text.Encoding.Default.GetBytes(content);
            barcode.SetColorDepth(nColor);
            barcode.MakeBinary(b, "temp.bmp",nClumn, nErr, nHLRatio, nHeight);
            barcode.MixText("temp.bmp", "temp.bmp", szText, lFontSize, lTxtHeight, lHmargin, lVmargin, lTxtCntOneLine);
            pictureBox1.ImageLocation = "temp.bmp";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string fileName = "";
            SaveFileDialog save = new SaveFileDialog();
            //FileStream fs = null;
            save.Title = "保存文件";
            save.Filter = "Bmp Files(*.bmp)|*.bmp|All Files(*.*)|*.*";
            save.FilterIndex = 1;
            save.RestoreDirectory = true;
            save.AddExtension = true;
            //save.
            save.FileName = "temp.bmp";
            save.InitialDirectory = Application.ExecutablePath;
            save.OverwritePrompt = true;
            if(save.ShowDialog()==DialogResult.OK){
                fileName = save.FileName;
                try{
                    //if(!fileName.Equals(Application.ExecutablePath)){
                    File.Delete(fileName);
                    //}
                    File.Copy("temp.bmp", fileName);
                }catch(Exception excep){
                    MessageBox.Show(excep.Message);
                }
                
                //fs.Close();

            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int nClumn = Int32.Parse(textBox1.Text);
            int nErr = Int32.Parse(textBox2.Text);
            int nHLRatio = Int32.Parse(textBox3.Text);
            int nHeight = Int32.Parse(textBox5.Text);
            int nColor = Int32.Parse(textBox4.Text);
            string szText = textBox8.Text;
            int lFontSize = Int32.Parse(textBox9.Text);
            int lTxtHeight = Int32.Parse(textBox10.Text);
            int lHmargin = Int32.Parse(textBox11.Text);
            int lVmargin = Int32.Parse(textBox12.Text);
            int lTxtCntOneLine = Int32.Parse(textBox7.Text);
            FileStream fs=null;
            StreamReader reader=null;
            string fileName = "";
            string content = "";
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "打开文件";
            open.InitialDirectory =Application.ExecutablePath;
            open.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*";
            open.FilterIndex = 1;
            open.RestoreDirectory = true;
            if(open.ShowDialog()==DialogResult.OK){
                fileName = open.FileName;
                try{
                    fs = new FileStream(fileName, FileMode.Open);
                    reader = new StreamReader(fs,Encoding.Default);
                    content = reader.ReadToEnd();
                    
                }catch(Exception excep){
                    MessageBox.Show(excep.Message);
                    return;
                }
                finally{
                    reader.Close();
                    fs.Close();
                }
                //MessageBox.Show(content);
                byte[] b = System.Text.Encoding.Default.GetBytes(content);

                //MessageBox.Show(""+b.Length);
                barcode.SetColorDepth(nColor);
                barcode.MakeBinary(b, "temp.bmp", nClumn, nErr, nHLRatio, nHeight);
                barcode.MixText("temp.bmp", "temp.bmp", szText, lFontSize, lTxtHeight, lHmargin, lVmargin, lTxtCntOneLine);
                pictureBox1.ImageLocation = "temp.bmp";
            }
        }

    }
}