// initialization
        private void OnLoad(object sender, EventArgs e)
        {
            // program title
            Text = "Pdf417DecoderDemo - " + Pdf417Decoder.VersionNumber + " \u00a9 2019 Uzi Granot. All rights reserved.";

                #if DEBUG
            // current directory
            string CurDir  = Environment.CurrentDirectory;
            string WorkDir = CurDir.Replace("bin\\Debug", "Work");
            if (WorkDir != CurDir && Directory.Exists(WorkDir))
            {
                Environment.CurrentDirectory = WorkDir;
            }

            // open trace file
            Pdf417Trace.Open("Pdf417DecoderTrace.txt");
            Pdf417Trace.Write("Pdf417DecoderDemo");
                #endif

            // create decoder
            Pdf417Decoder = new Pdf417Decoder();

            // resize window
            OnResize(sender, e);
            return;
        }
        private void VideoDecoderTimer_Tick(object sender, EventArgs e)
        {
            VideoDecoderTimer.Enabled = false;
            Bitmap Pdf417Image;

            try
            {
                Pdf417Image = VideoCamera.SnapshotSourceImage();

                // trace
                        #if DEBUG
                Pdf417Trace.Format("Image width: {0}, Height: {1}", Pdf417Image.Width, Pdf417Image.Height);
                        #endif
            }

            catch (Exception EX)
            {
                DataTextBox.Text          = "Decode exception.\r\n" + EX.Message;
                VideoDecoderTimer.Enabled = true;
                return;
            }

            // decode image to byte array (Pdf417Decoder.BarcodeBinaryData)
            string BarcodeText = null;

            // decode barcodes
            int BarcodesCount = Decoder.Decode(Pdf417Image);

            // dispose bitmap
            Pdf417Image.Dispose();

            // we have no Pdf417 barcode
            if (BarcodesCount == 0)
            {
                VideoDecoderTimer.Enabled = true;
                return;
            }

            // decoding was successful
            // convert binary data to text string
            BarcodeText = Decoder.BinaryDataToString(0);

            // we have no Pdf417 barcode
            if (BarcodeText == null || BarcodeText.Length == 0)
            {
                VideoDecoderTimer.Enabled = true;
                return;
            }

            VideoCamera.PauseGraph();

            DataTextBox.Text    = BarcodeText;
            ResetButton.Enabled = true;
            if (IsValidUri(DataTextBox.Text))
            {
                GoToUriButton.Enabled = true;
            }
            return;
        }
        /// <summary>
        /// Program initialization
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        private void OnLoad(object sender, EventArgs e)
        {
            // program title
            Text = "Pdf417VideoDecoder - " + Pdf417Decoder.VersionNumber + " \u00a9 2019 Uzi Granot. All rights reserved.";

                #if DEBUG
            // current directory
            string CurDir  = Environment.CurrentDirectory;
            string WorkDir = CurDir.Replace("bin\\Debug", "Work");
            if (WorkDir != CurDir && Directory.Exists(WorkDir))
            {
                Environment.CurrentDirectory = WorkDir;
            }

            // open trace file
            Pdf417Trace.Open("Pdf417VideoDecoderTrace.txt");
            Pdf417Trace.Write(Text);
                #endif

            // disable reset button
            ResetButton.Enabled   = false;
            GoToUriButton.Enabled = false;

            // get an array of web camera devices
            DsDevice[] CameraDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

            // make sure at least one is available
            if (CameraDevices == null || CameraDevices.Length == 0)
            {
                MessageBox.Show("No video cameras in this computer");
                Close();
                return;
            }

            // select the first camera
            DsDevice CameraDevice = CameraDevices[0];

            // Device moniker
            IMoniker CameraMoniker = CameraDevice.Moniker;

            // get a list of frame sizes available
            FrameSize[] FrameSizes = Camera.GetFrameSizeList(CameraMoniker);

            // make sure there is at least one frame size
            if (FrameSizes == null || FrameSizes.Length == 0)
            {
                MessageBox.Show("No video cameras in this computer");
                Close();
                return;
            }

            // test if our frame size is available
            int Index;
            for (Index = 0; Index < FrameSizes.Length &&
                 (FrameSizes[Index].Width != FrameSize.Width || FrameSizes[Index].Height != FrameSize.Height); Index++)
            {
                ;
            }

            // select first frame size
            if (Index == FrameSizes.Length)
            {
                FrameSize = FrameSizes[0];
            }

            // Set selected camera to camera control with default frame size
            // Create camera object
            VideoCamera = new Camera(PreviewPanel, CameraMoniker, FrameSize);

            // create QR code decoder
            Decoder = new Pdf417Decoder();

            // resize window
            OnResize(sender, e);

            // create timer
            VideoDecoderTimer          = new Timer();
            VideoDecoderTimer.Interval = 200;
            VideoDecoderTimer.Tick    += VideoDecoderTimer_Tick;
            VideoDecoderTimer.Enabled  = true;
            return;
        }
        // user pressed load image button
        private void OnLoadImage(object sender, EventArgs e)
        {
            // get file name to decode
            OpenFileDialog Dialog = new OpenFileDialog
            {
                Filter           = "Image Files(*.png;*.jpg;*.gif;*.tif)|*.png;*.jpg;*.gif;*.tif;*.bmp)|All files (*.*)|*.*",
                Title            = "Load PDF417 Barcode Image",
                InitialDirectory = Directory.GetCurrentDirectory(),
                RestoreDirectory = true,
                FileName         = string.Empty
            };

            // display dialog box
            if (Dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            // display file name
            int Ptr  = Dialog.FileName.LastIndexOf('\\');
            int Ptr1 = Dialog.FileName.LastIndexOf('\\', Ptr - 1);

            ImageFileLabel.Text = Dialog.FileName.Substring(Ptr1 + 1);

            // disable buttons
            LoadImageButton.Enabled = false;

            // dispose previous image
            if (Pdf417InputImage != null)
            {
                Pdf417InputImage.Dispose();
            }

            // load image to bitmap
            Pdf417InputImage = new Bitmap(Dialog.FileName);

            // trace
                #if DEBUG
            Pdf417Trace.Format("****");
            Pdf417Trace.Format("Decode image: {0} ", Dialog.FileName);
            Pdf417Trace.Format("Image width: {0}, Height: {1}", Pdf417InputImage.Width, Pdf417InputImage.Height);
                #endif

            // decode barcodes
            int BarcodesCount = Pdf417Decoder.Decode(Pdf417InputImage);

            // no barcodes were found
            if (BarcodesCount == 0)
            {
                        #if DEBUG
                Pdf417Trace.Format("Image has no barcodes: {0}", Dialog.FileName);
                        #endif

                // clear barcode data text box
                DataTextBox.Text = null;
                MessageBox.Show(string.Format("Image has no barcodes: {0}", Dialog.FileName));
            }

            // one barcodes was found
            else if (BarcodesCount == 1)
            {
                // decoding was successful
                // convert binary data to text string
                DataTextBox.Text = Pdf417Decoder.BinaryDataToString(0);
            }

            // more than one barcode
            else
            {
                StringBuilder Str = new StringBuilder();

                for (int Count = 0; Count < BarcodesCount; Count++)
                {
                    Str.AppendFormat("Barcode No. {0}\r\n{1}\r\n", Count + 1, Pdf417Decoder.BinaryDataToString(Count));
                }
                DataTextBox.Text = Str.ToString();
            }

            // enable buttons
            LoadImageButton.Enabled = true;

            // force repaint
            Invalidate();
            return;
        }