using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
using DibalImage;


namespace DibalImageDemoCSharp
{
    #region STRUCTURES

    public struct DibalImageStruct
    {
        public string path;
        public int type;
        public int id;
        public int assignPlu;

        public DibalImageStruct(string _path, int _type, int _id, int _assignPlu)
        {
            this.path = _path;
            this.type = _type;
            this.id = _id;
            this.assignPlu = _assignPlu;
        }
    }

    #endregion
    

    

    public partial class Form1 : Form
    {
        #region VARIABLES

        string rutaImage = string.Empty;
        DataSet ds = new DataSet();
        const string MODEL500RANGE = "500RANGE";
        const string MODELLSERIES = "LSERIES";

        #endregion


        #region PUBLIC METHODS

        public Form1()
        {
            InitializeComponent();
            DataLoad();


        }

        #endregion

        #region PRIVATED METHODS

        private void DataLoad()
        {

            string versionDemo = System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion;
            versionDemo = versionDemo.Replace(" ", "");
            this.Text = "DibalImageDemoC# v" + versionDemo;
            this.lblVersion.Text = "ImageSDK v 1.0.0.2";
#if UNICODE
            this.Text += " (UNICODE)";
            this.lblVersion.Text += " (UNICODE)";
#endif
            //Get Diabalscop version
            string versionDibalImage = System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.StartupPath + "\\DibalImage.dll").FileVersion;
            versionDibalImage = versionDibalImage.Replace(" ", "");
            versionDibalImage = versionDibalImage.Replace(',', '.');
            this.lblDibalImageVersion.Text = "DibalImage.dll v" + versionDibalImage;

            //Create Image Scale table
            this.grvImageScales.AutoGenerateColumns = false;

            this.ds.Tables.Add(new DataTable("ImageScales"));
            this.ds.Tables["ImageScales"].Columns.Add(new DataColumn("ImgMasterAddress"));
            this.ds.Tables["ImageScales"].Columns.Add(new DataColumn("ImgGroup"));
            this.ds.Tables["ImageScales"].Columns.Add(new DataColumn("ImgIpAddress"));
            this.ds.Tables["ImageScales"].Columns.Add(new DataColumn("ImgReceptionPortRx"));
            this.ds.Tables["ImageScales"].Columns.Add(new DataColumn("ImgModel"));
            this.ds.Tables["ImageScales"].Columns.Add(new DataColumn("ImgDisplaySize"));
            this.grvImageScales.DataSource = this.ds.Tables["ImageScales"];


            //Create Image Model column
            this.ds.Tables.Add(new DataTable("ImageScalesModel"));
            this.ds.Tables["ImageScalesModel"].Columns.Add(new DataColumn("imgModelValue"));
            this.ds.Tables["ImageScalesModel"].Columns.Add(new DataColumn("imgModelType"));
            DataRow drImgModel = this.ds.Tables["ImageScalesModel"].NewRow();
            drImgModel[0] = "0";
            drImgModel[1] = "500RANGE";
            this.ds.Tables["ImageScalesModel"].Rows.Add(drImgModel);
            drImgModel = this.ds.Tables["ImageScalesModel"].NewRow();
            drImgModel[0] = "1";
            drImgModel[1] = "LSERIES";
            this.ds.Tables["ImageScalesModel"].Rows.Add(drImgModel);

            this.ImgModel.ValueMember = this.ds.Tables["ImageScalesModel"].Columns["imgModelValue"].ToString();
            this.ImgModel.DisplayMember = this.ds.Tables["ImageScalesModel"].Columns["imgModelType"].ToString();
            this.ImgModel.DataSource = this.ds.Tables["ImageScalesModel"];

            this.ImgMasterAddress.DataPropertyName = "ImgMasterAddress";
            this.ImgGroup.DataPropertyName = "ImgGroup";
            this.ImgIpAddress.DataPropertyName = "ImgIpAddress";
            this.ImgReceptionPortRx.DataPropertyName = "ImgReceptionPortRx";
            this.ImgModel.DataPropertyName = "ImgModel";
            this.grvImageScales.DataSource = this.ds.Tables["ImageScales"];

            //Create Image DisplaySize column
            this.ds.Tables.Add(new DataTable("ImageScalesDisplaySize"));
            this.ds.Tables["ImageScalesDisplaySize"].Columns.Add(new DataColumn("imgDisplaySizeValue"));
            this.ds.Tables["ImageScalesDisplaySize"].Columns.Add(new DataColumn("imgDisplaySizeType"));
            DataRow drImgDisplaySize = this.ds.Tables["ImageScalesDisplaySize"].NewRow();
            drImgDisplaySize[0] = "0";
            drImgDisplaySize[1] = "7 Inches";
            this.ds.Tables["ImageScalesDisplaySize"].Rows.Add(drImgDisplaySize);
            drImgDisplaySize = this.ds.Tables["ImageScalesDisplaySize"].NewRow();
            drImgDisplaySize[0] = "1";
            drImgDisplaySize[1] = "12 Inches";
            this.ds.Tables["ImageScalesDisplaySize"].Rows.Add(drImgDisplaySize);
            drImgDisplaySize = this.ds.Tables["ImageScalesDisplaySize"].NewRow();
            drImgDisplaySize[0] = "2";
            drImgDisplaySize[1] = "15 Inches";
            this.ds.Tables["ImageScalesDisplaySize"].Rows.Add(drImgDisplaySize);


            this.ImgDisplaySize.ValueMember = this.ds.Tables["ImageScalesDisplaySize"].Columns["imgDisplaySizeValue"].ToString();
            this.ImgDisplaySize.DisplayMember = this.ds.Tables["ImageScalesDisplaySize"].Columns["imgDisplaySizeType"].ToString();
            this.ImgDisplaySize.DataSource = this.ds.Tables["ImageScalesDisplaySize"];

            this.ImgMasterAddress.DataPropertyName = "ImgMasterAddress";
            this.ImgGroup.DataPropertyName = "ImgGroup";
            this.ImgIpAddress.DataPropertyName = "ImgIpAddress";
            this.ImgReceptionPortRx.DataPropertyName = "ImgReceptionPortRx";
            this.ImgModel.DataPropertyName = "ImgModel";
            this.ImgDisplaySize.DataPropertyName = "ImgDisplaySize";
            this.grvImageScales.DataSource = this.ds.Tables["ImageScales"];

            //Create Images table
            this.grvImages.AutoGenerateColumns = false;

            this.ds.Tables.Add(new DataTable("Images"));
            this.ds.Tables["Images"].Columns.Add(new DataColumn("ImgPath"));
            this.ds.Tables["Images"].Columns.Add(new DataColumn("ImgId"));
            this.ds.Tables["Images"].Columns.Add(new DataColumn("ImgType"));

            //Create Image Type column
            this.ds.Tables.Add(new DataTable("ImageType"));
            this.ds.Tables["ImageType"].Columns.Add(new DataColumn("typeValue"));
            this.ds.Tables["ImageType"].Columns.Add(new DataColumn("typeType"));
            DataRow drImgType = this.ds.Tables["ImageType"].NewRow();
            drImgType[0] = "1";
            drImgType[1] = "PUBLICITY";
            this.ds.Tables["ImageType"].Rows.Add(drImgType);
            drImgType = this.ds.Tables["ImageType"].NewRow();
            drImgType[0] = "2";
            drImgType[1] = "ARTICLE/ORDER";
            this.ds.Tables["ImageType"].Rows.Add(drImgType);
            drImgType = this.ds.Tables["ImageType"].NewRow();
            drImgType[0] = "0";
            drImgType[1] = "ARTICLE/PLU";
            this.ds.Tables["ImageType"].Rows.Add(drImgType);

            this.ImgType.ValueMember = this.ds.Tables["ImageType"].Columns["typeValue"].ToString();
            this.ImgType.DisplayMember = this.ds.Tables["ImageType"].Columns["typeType"].ToString();
            this.ImgType.DataSource = this.ds.Tables["ImageType"];

            this.ImgPath.DataPropertyName = "ImgPath";
            this.ImgId.DataPropertyName = "ImgId";
            this.ImgType.DataPropertyName = "ImgType";
            this.grvImages.DataSource = this.ds.Tables["Images"];

            DataRow datar = this.ds.Tables["Images"].NewRow();
            this.ds.Tables["Images"].Rows.Add(datar);
            this.grvImages.DataSource = this.ds.Tables["Images"];


            this.ds.Tables.Add(new DataTable("ImageFolder"));
            this.ds.Tables["ImageFolder"].Columns.Add(new DataColumn("ImgPathFolder"));
            this.ds.Tables["ImageFolder"].Columns.Add(new DataColumn("ImgTypeFolder"));


            this.ds.Tables.Add(new DataTable("ImageTypeFolder"));
            this.ds.Tables["ImageTypeFolder"].Columns.Add(new DataColumn("typeValue"));
            this.ds.Tables["ImageTypeFolder"].Columns.Add(new DataColumn("typeType"));
            DataRow drImgTypeF = this.ds.Tables["ImageTypeFolder"].NewRow();
            drImgTypeF[0] = "1";
            drImgTypeF[1] = "PUBLICITY";
            this.ds.Tables["ImageTypeFolder"].Rows.Add(drImgTypeF);
            drImgTypeF = this.ds.Tables["ImageTypeFolder"].NewRow();
            drImgTypeF[0] = "2";
            drImgTypeF[1] = "ARTICLE/ORDER";
            this.ds.Tables["ImageTypeFolder"].Rows.Add(drImgTypeF);
            drImgTypeF = this.ds.Tables["ImageTypeFolder"].NewRow();
            drImgTypeF[0] = "0";
            drImgTypeF[1] = "ARTICLE/PLU";
            this.ds.Tables["ImageTypeFolder"].Rows.Add(drImgTypeF);

            this.ImgTypeFolder.ValueMember = this.ds.Tables["ImageTypeFolder"].Columns["typeValue"].ToString();
            this.ImgTypeFolder.DisplayMember = this.ds.Tables["ImageTypeFolder"].Columns["typeType"].ToString();
            this.ImgTypeFolder.DataSource = this.ds.Tables["ImageTypeFolder"];

            this.ImgPathFolder.DataPropertyName = "ImgPathFolder";
            this.ImgTypeFolder.DataPropertyName = "ImgTypeFolder";
            DataRow datar1 = this.ds.Tables["ImageFolder"].NewRow();

            this.ds.Tables["ImageFolder"].Rows.Add(datar1);
            this.grvImageFolder.DataSource = this.ds.Tables["ImageFolder"];

            this.chkImageFile.Checked = true;
            this.chkImageFolder.Checked = true;
            this.chkImageFolder.Checked = false;

            this.grvImages.Columns["ImgPath"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.grvImageFolder.Columns["ImgPathFolder"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.grvImages.Columns["ImgPath"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.grvImageFolder.Columns["ImgPathFolder"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            //this.grvImageFolder.Rows.Add();
            this.grvImageFolder.ClearSelection();
            this.grvImages.ClearSelection();
        }

        #endregion

        #region EVENTS

        


        #endregion

        #region RGISTERS CREATION METHODS

        private void GenerateImageScales()
        {
            try
            {
                int numScales = 0;
                //Example values
                string IpAddress = "192.168.1.";
                int Rxport = 3000;

                //delete result rows
                this.ds.Tables["ImageScales"].Rows.Clear();
                int.TryParse(this.txtImgNScales.Text, out numScales);

                //Add scales to gridView
                this.ds.Tables["ImageScales"].BeginLoadData();
                for (int j = 1; j <= numScales; j++)
                {
                    DataRow dr = ds.Tables["ImageScales"].NewRow();

                    dr["ImgMasterAddress"] = Convert.ToString((j * 2) - 2);
                    dr["ImgIpAddress"] = Convert.ToString(IpAddress + j);
                    dr["ImgReceptionPortRx"] = Convert.ToString(Rxport);
                    dr["ImgModel"] = 0;
                    dr["ImgGroup"] = Convert.ToString(0);
                    dr["ImgDisplaySize"] = 0;

                    this.ds.Tables["ImageScales"].Rows.Add(dr);
                }
                this.ds.Tables["ImageScales"].EndLoadData();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private ImagePalletized.DibalScale[] GetScales(DataTable dtScales)
        {
            try
            {
                ImagePalletized.DibalScale scale;
                ImagePalletized.DibalScale[] myScales = new ImagePalletized.DibalScale[dtScales.Rows.Count];
                ArrayList arlScale = new ArrayList();
                //Default Scale variables
                int scaleMasterAddressAux = 0;
                string scaleIpAddressAux = string.Empty;
                int scalePortRxAux = 3000;
                int scalePortTxAux = 3001;
                string scaleModelAux = MODEL500RANGE;
                string scaleDisplayAux = string.Empty;
                string scaleSectionsAux = string.Empty;
                int scaleGroupAux = 0;
                string scaleLogsPathAux = string.Empty;

                foreach (DataRow dr in dtScales.Rows)
                {


                    //Cast DBnull of DataGrid
                    int.TryParse(Convert.ToString(dr["ImgMasterAddress"]), out scaleMasterAddressAux);
                    int.TryParse(Convert.ToString(dr["ImgGroup"]), out scaleGroupAux);
                    if (!string.IsNullOrEmpty(Convert.ToString(dr["ImgIpAddress"])))
                        scaleIpAddressAux = Convert.ToString(dr["ImgIpAddress"]);
                    int.TryParse(Convert.ToString(dr["ImgReceptionPortRx"]), out scalePortRxAux);

                    if (!string.IsNullOrEmpty(Convert.ToString(dr["ImgModel"])))
                    {
                        if (Convert.ToString(dr["ImgModel"]) == "0")
                            scaleModelAux = MODEL500RANGE;
                        else
                            scaleModelAux = MODELLSERIES;
                    }

                    //Create a scale
                    scale = new ImagePalletized.DibalScale(scaleMasterAddressAux, scaleIpAddressAux, scalePortTxAux, scalePortRxAux, scaleModelAux, scaleDisplayAux, scaleSectionsAux, scaleGroupAux, scaleLogsPathAux);
                    //Add a scale to ArrayList "arlScale"
                    arlScale.Add(scale);
                    //Copy the Scale objects of the ArrayList "arlScale" to a Scale array "myScales"
                    myScales = (ImagePalletized.DibalScale[])arlScale.ToArray(typeof(ImagePalletized.DibalScale));
                }
                return myScales;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private DibalImageStruct[] GetImages()
        {
            try
            {
                DibalImageStruct image = new DibalImageStruct();
                DibalImageStruct[] myImage;
                ArrayList arlImage = new ArrayList();
                int type = 0;
                int plu = 0;



                if (chkImageFile.Checked)
                {
                    myImage = new DibalImageStruct[ds.Tables["Images"].Rows.Count];
                    foreach (DataRow dr in ds.Tables["Images"].Rows)
                    {
                        if (!string.IsNullOrEmpty(Convert.ToString(dr["ImgPath"])) && !string.IsNullOrEmpty(Convert.ToString(dr["ImgId"])) && !string.IsNullOrEmpty(Convert.ToString(dr["ImgType"])))
                        {
                            if (Convert.ToInt32(dr["ImgType"]) == 0)
                            {
                                type = 2;
                                plu = 1;
                            }
                            else if (Convert.ToInt32(dr["ImgType"]) == 2)
                            {
                                type = 2;
                                plu = 0;
                            }
                            else
                            {
                                type = 1;
                                plu = 0;
                            }
                            image = new DibalImageStruct(Convert.ToString(dr["ImgPath"]), type, Convert.ToInt32(dr["ImgId"]), plu);
                            arlImage.Add(image);
                            myImage = (DibalImageStruct[])arlImage.ToArray(typeof(DibalImageStruct));
                        }
                    }
                }
                else
                {
                    myImage = new DibalImageStruct[ds.Tables["ImageFolder"].Rows.Count];
                    foreach (DataRow dr in ds.Tables["ImageFolder"].Rows)
                    {
                        if (!string.IsNullOrEmpty(Convert.ToString(dr["ImgPathFolder"])) && !string.IsNullOrEmpty(Convert.ToString(dr["ImgTypeFolder"])))
                        {
                            if (Convert.ToInt32(dr["ImgTypeFolder"]) == 0)
                            {
                                type = 2;
                                plu = 1;
                            }
                            else if (Convert.ToInt32(dr["ImgType"]) == 2)
                            {
                                type = 2;
                                plu = 0;
                            }
                            else
                            {
                                type = 1;
                                plu = 0;
                            }
                            image = new DibalImageStruct(Convert.ToString(dr["ImgPathFolder"]), type, 0, plu);
                            arlImage.Add(image);
                            myImage = (DibalImageStruct[])arlImage.ToArray(typeof(DibalImageStruct));
                        }
                    }
                }
                return myImage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        

        #endregion

        #region REGISTERS CREATION EVENTS
        private void chkImageFile_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkImageFile.Checked == true)
            {
                label3.Enabled = true;
                this.chkImageFolder.Checked = false;
                grvImages.Enabled = true;
                groupBox13.Enabled = true;
            }
            else
            {
                label3.Enabled = false;
                this.grvImages.Enabled = false;
                this.chkImageFolder.Checked = true;
                groupBox13.Enabled = false;
            }

        }

        private void chkImageFolder_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkImageFolder.Checked == true)
            {
                label4.Enabled = true;
                this.chkImageFile.Checked = false;
                grvImageFolder.Enabled = true;
                groupBox13.Enabled = false;
            }
            else
            {
                label4.Enabled = false;
                this.grvImageFolder.Enabled = false;
                this.chkImageFile.Checked = true;
                groupBox13.Enabled = true;

            }

        }
        private void btnImgGenerateScales_Click(object sender, EventArgs e)
        {
            GenerateImageScales();
        }

        private void btnImgDeleteScales_Click(object sender, EventArgs e)
        {
            this.ds.Tables["ImageScales"].Rows.Clear();
        }

        private void txtImgNScales_KeyUp(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                GenerateImageScales();
            }
        }

        //private void btnImagefileGenerator_Click_1(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        DibalImageStruct[] myImages = new DibalImageStruct[ds.Tables["Images"].Rows.Count];
        //        int concatenate = 0;
        //        lblResult.Text = "";


        //        if (ds.Tables["ImageScales"].Rows.Count > 0 && ds.Tables["Images"].Rows.Count > 0)
        //        {
        //            SaveFileDialog sfd1 = new SaveFileDialog();
        //            sfd1.AddExtension = true;
        //            sfd1.DefaultExt = "txt";
        //            sfd1.InitialDirectory = @"C:\";
        //            sfd1.RestoreDirectory = true;
        //            sfd1.Title = "Save Document as";
        //            sfd1.Filter = "txt files (*.txt)|*.txt";
        //            sfd1.CheckPathExists = true;
        //            if (rdbConcatenate.Checked)
        //            {
        //                sfd1.OverwritePrompt = false;
        //                concatenate = 1;
        //            }
        //            else
        //            {
        //                sfd1.OverwritePrompt = true;
        //                if (ds.Tables["ImageScales"].Rows.Count <= 1)
        //                {
        //                    concatenate = 0;
        //                }
        //            }
        //            if (sfd1.ShowDialog() == DialogResult.OK)
        //            {
        //                //Create Document                            
        //                myImages = GetImages();


        //                //Here you have to call to the function "ImageFileGenerator" to create registers.
        //                foreach (DataRow dr in ds.Tables["ImageScales"].Rows)
        //                {

        //                    foreach (DibalImageStruct di in myImages)
        //                    {
        //                        GraphicImage.ImagePalletized.ImageFileGenerator(Convert.ToInt16(dr["ImgMasterAddress"]), di.type, di.id, di.path, sfd1.FileName, concatenate, di.assignPlu, Convert.ToInt16(dr["ImgDisplaySize"]));
        //                    }

        //                }

        //            }

        //            if (System.IO.File.Exists(sfd1.FileName))
        //            {
        //                Process.Start(sfd1.FileName);
        //                lblResult.Text = "Document Created";
        //            }
        //            else
        //            {
        //                lblResult.Text = "Document NOT Created";
        //            }

        //            this.btnImagefileGenerator.Enabled = true;
        //            this.btnImageRegisterGenerator.Enabled = true;

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }

        //}
        private void btnImagefileGenerator_Click(object sender, EventArgs e)
        {
            try
            {
                DibalImageStruct[] myImages = new DibalImageStruct[ds.Tables["Images"].Rows.Count];
                int concatenate = 0;


                SaveFileDialog sfd1 = new SaveFileDialog();

                if (ds.Tables["ImageScales"].Rows.Count > 0 && ds.Tables["Images"].Rows.Count > 0)
                {
                    if (rdbConcatenate.Checked)
                    {
                        sfd1.OverwritePrompt = false;
                        concatenate = 1;
                    }
                    else
                    {
                        sfd1.OverwritePrompt = true;
                        if (ds.Tables["ImageScales"].Rows.Count <= 1)
                        {
                            concatenate = 0;
                        }
                    }
                    this.btnImageRegisterGenerator.Enabled = false;
                    this.btnImagefileGenerator.Enabled = false;
                    if (concatenate == 1)
                        rdbReplace.Checked = false;

                    else
                        rdbReplace.Checked = true;

                    sfd1.AddExtension = true;
                    sfd1.DefaultExt = "txt";
                    sfd1.InitialDirectory = @"C:\";
                    sfd1.RestoreDirectory = true;
                    sfd1.Title = "Save Document as";
                    sfd1.Filter = "txt files (*.txt)|*.txt";
                    sfd1.CheckPathExists = true;

                    if (sfd1.ShowDialog() == DialogResult.OK)
                    {
                        //Create Document
                        

                        myImages = GetImages();
                        //Delete result rows
                        

                        //Here you have to call to the function "ImageFileGenerator" to create registers.
                        foreach (DataRow dr in ds.Tables["ImageScales"].Rows)
                        {

                            foreach (DibalImageStruct di in myImages)
                            {
                                DibalImage.ImagePalletized.ImageFileGeneratorBytes(Convert.ToInt32(dr["ImgMasterAddress"]), di.type, di.id, di.path, sfd1.FileName, concatenate, di.assignPlu, Convert.ToInt32(dr["ImgDisplaySize"]));
                            }

                        }

                    }

                    

                    this.btnImagefileGenerator.Enabled = true;
                    this.btnImageRegisterGenerator.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void btnImageRegisterGenerator_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable imageScales = ds.Tables["ImageScales"];
                DibalImage.ImagePalletized.DibalScale[] myImageScales = new DibalImage.ImagePalletized.DibalScale[ds.Tables["ImageScales"].Rows.Count];
                ArrayList mySevenInchScales = new ArrayList();
                ArrayList myTwelveInchScales = new ArrayList();
                ArrayList myFifteenInchScales = new ArrayList();
                DibalImageStruct[] myImages;
                DibalImage.ImagePalletized.ImageStruct[] myImagesRegisters = new DibalImage.ImagePalletized.ImageStruct[ds.Tables["Images"].Rows.Count * ds.Tables["ImageScales"].Rows.Count];
                DibalImage.ImagePalletized.ImageStruct image = new DibalImage.ImagePalletized.ImageStruct();
                string Result = string.Empty;
                string imageRegister = string.Empty;
                int con = 0;
                int row = 0;
                if (chkImageFile.Checked)
                    myImages = new DibalImageStruct[ds.Tables["Images"].Rows.Count];
                else
                    myImages = new DibalImageStruct[ds.Tables["ImageFolder"].Rows.Count];


                //delete result rows
                

                this.btnImagefileGenerator.Enabled = false;
                this.btnImageRegisterGenerator.Enabled = false;

                myImageScales = GetScales(ds.Tables["ImageScales"]);
                myImages = GetImages();

                

                //Separate scales by display size
                foreach (DataRow dr in imageScales.Rows)
                {
                    if (Convert.ToInt32(dr["ImgDisplaySize"]) == 0)
                    {
                        mySevenInchScales.Add(myImageScales[row]);
                        row++;
                    }
                    else if (Convert.ToInt32(dr["ImgDisplaySize"]) == 1)
                    {
                        myTwelveInchScales.Add(myImageScales[row]);
                        row++;
                    }
                    else if (Convert.ToInt32(dr["ImgDisplaySize"]) == 2)
                    {
                        myFifteenInchScales.Add(myImageScales[row]);
                        row++;
                    }
                }
                //Here you have to call to the function "ImageRegisterGenerator" to create registers and "ImageSend" or "MultiImageSend" to send them to scales.                
                //send 7 Inch scales
                if (mySevenInchScales.Count > 0)
                {


                    DibalImage.ImagePalletized.DibalScale[] sevenScales = (DibalImage.ImagePalletized.DibalScale[])mySevenInchScales.ToArray(typeof(DibalImage.ImagePalletized.DibalScale));

                    if (sevenScales.Length == 1)
                    {
                        foreach (DibalImageStruct di in myImages)
                        {
                            if (chkImageFolder.Checked) //It is a folder
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendMultiImagestoSingleScale(sevenScales[0], di.type, di.path, di.assignPlu, 0, 1, 2);
                            }
                            else
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendImagetoSingleScale(sevenScales[0], di.type, di.id, di.path, di.assignPlu, 0, 1, 2);
                            }
                        }
                    }
                    else
                    {
                        foreach (DibalImageStruct di in myImages)
                        {
                            if (chkImageFolder.Checked) //It is a folder
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendMultiImagestoMultiScales(sevenScales, sevenScales.Length, di.type, di.path, di.assignPlu, 0, 1, 2);
                            }
                            else
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendImagetoMultiScales(sevenScales, sevenScales.Length, di.type, di.id, di.path, di.assignPlu, 0, 1, 2);
                            }
                        }
                    }
                }
                //Send 12 Inch scales
                if (myTwelveInchScales.Count > 0)
                {
                    DibalImage.ImagePalletized.DibalScale[] twelveScales = ((DibalImage.ImagePalletized.DibalScale[])myTwelveInchScales.ToArray(typeof(DibalImage.ImagePalletized.DibalScale)));

                    if (twelveScales.Length == 1)
                    {
                        foreach (DibalImageStruct di in myImages)
                        {
                            if (chkImageFolder.Checked) //It is a folder
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendMultiImagestoSingleScale(twelveScales[0], di.type, di.path, di.assignPlu, 0, 1, 2);
                            }
                            else
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendImagetoSingleScale(twelveScales[0], di.type, di.id, di.path, di.assignPlu, 0, 1, 2);
                            }
                        }
                    }
                    else
                    {
                        foreach (DibalImageStruct di in myImages)
                        {
                            if (chkImageFolder.Checked) //It is a folder
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendMultiImagestoMultiScales(twelveScales, twelveScales.Length, di.type, di.path, di.assignPlu, 0, 1, 2);
                            }
                            else
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendImagetoMultiScales(twelveScales, twelveScales.Length, di.type, di.id, di.path, di.assignPlu, 0, 1, 2);
                            }
                        }
                    }
                }
                //Send 15 Inch scales
                if (myFifteenInchScales.Count > 0)
                {
                    DibalImage.ImagePalletized.DibalScale[] fifteenScales = ((DibalImage.ImagePalletized.DibalScale[])myFifteenInchScales.ToArray(typeof(DibalImage.ImagePalletized.DibalScale)));

                    if (fifteenScales.Length == 1)
                    {
                        foreach (DibalImageStruct di in myImages)
                        {
                            if (chkImageFolder.Checked) //It is a folder
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendMultiImagestoSingleScale(fifteenScales[0], di.type, di.path, di.assignPlu, 0, 1, 2);
                            }
                            else
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendImagetoSingleScale(fifteenScales[0], di.type, di.id, di.path, di.assignPlu, 0, 1, 2);
                            }
                        }
                    }
                    else
                    {
                        foreach (DibalImageStruct di in myImages)
                        {
                            if (chkImageFolder.Checked) //It is a folder
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendMultiImagestoMultiScales(fifteenScales, fifteenScales.Length, di.type, di.path, di.assignPlu, 0, 1, 2);
                            }
                            else
                            {
                                if (di.path != null)
                                    Result = DibalImage.ImagePalletized.SendImagetoMultiScales(fifteenScales, fifteenScales.Length, di.type, di.id, di.path, di.assignPlu, 0, 1, 2);
                            }
                        }
                    }
                }

                

                this.btnImageRegisterGenerator.Enabled = true;
                this.btnImagefileGenerator.Enabled = true;
                MessageBox.Show(Result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void grvImages_SelectionChanged(object sender, EventArgs e)
        {
            grvImages.ClearSelection();
        }

        private void grvImageFolder_SelectionChanged(object sender, EventArgs e)
        {
            grvImageFolder.ClearSelection();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        

        

        //private void btnImageRegisterGenerator_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        DataTable imageScales = ds.Tables["ImageScales"];
        //        DibalScale[] myImageScales = new DibalScale[ds.Tables["ImageScales"].Rows.Count];
        //        ArrayList mySevenInchScales = new ArrayList();
        //        ArrayList myTwelveInchScales = new ArrayList();
        //        ArrayList myFifteenInchScales = new ArrayList();
        //        DibalImageStruct[] myImages = new DibalImageStruct[ds.Tables["Images"].Rows.Count];
        //        GraphicImage.ImagePalletized.ImageStruct image = new GraphicImage.ImagePalletized.ImageStruct();
        //        GraphicImage.ImagePalletized.imagBytes imageConv = new GraphicImage.ImagePalletized.imagBytes();
        //        string Result = string.Empty;
        //        string imageRegister = string.Empty;
        //        int row = 0;

        //        lblResult.Text = "";
        //        this.btnImageRegisterGenerator.Enabled = false;
        //        this.btnImagefileGenerator.Enabled = false;

        //        myImageScales = GetScales(ds.Tables["ImageScales"]);
        //        myImages = GetImages();


        //        //Separate scales by display size
        //        foreach (DataRow dr in imageScales.Rows)
        //        {
        //            if (Convert.ToInt32(dr["ImgDisplaySize"]) == 0)
        //            {
        //                mySevenInchScales.Add(myImageScales[row]);
        //            }
        //            else if (Convert.ToInt32(dr["ImgDisplaySize"]) == 1)
        //            {
        //                myTwelveInchScales.Add(myImageScales[row]);
        //            }
        //            else if (Convert.ToInt32(dr["ImgDisplaySize"]) == 2)
        //            {
        //                myFifteenInchScales.Add(myImageScales[row]);
        //            }
        //        }
        //        //Here you have to call to the function "ImageRegisterGenerator" to create registers and "ImageSend" or "MultiImageSend" to send them to scales.                
        //        if (mySevenInchScales.Count > 0)
        //        {
        //            foreach (DibalImageStruct di in myImages)
        //            {
        //                //image = GraphicImage.ImagePalletized.ImageRegisterGenerator(di.type, di.id, di.path, di.assignPlu, 0);
        //                //imageRegister += image.registers;

        //                imageConv = GraphicImage.ImagePalletized.ImageRegisterGeneratorBytes(di.type, di.id, di.path, di.assignPlu, 0);
                        
        //            }
        //        }

        //        if (myTwelveInchScales.Count > 0)
        //        {
        //            foreach (DibalImageStruct di in myImages)
        //            {
        //                image = GraphicImage.ImagePalletized.ImageRegisterGenerator(di.type, di.id, di.path, di.assignPlu, 1);
        //                imageRegister += image.registers;
        //            }
        //        }

        //        if (myFifteenInchScales.Count > 0)
        //        {
        //            foreach (DibalImageStruct di in myImages)
        //            {
        //                image = GraphicImage.ImagePalletized.ImageRegisterGenerator(di.type, di.id, di.path, di.assignPlu, 2);
        //                imageRegister += image.registers;
        //            }

        //        }

        //        if (imageRegister.Length > 0)
        //        {
        //            lblResult.Text = "Registers Generated";
        //        }


        //        this.btnImagefileGenerator.Enabled = true;
        //        this.btnImageRegisterGenerator.Enabled = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}


        #endregion




    }
}
