Example #1
0
        private void OnSaveClick(object sender, EventArgs e)
        {
            // reset some variables
            int CameraDistance            = 0;
            int CameraRotation            = 0;
            int ImageWidth                = 0;
            int ImageHeight               = 0;
            int QRImageDimension          = 0;
            int QRCodePosX                = 0;
            int QRCodePosY                = 0;
            int ViewXRotation             = 0;
            ErrorSpotControl ErrorControl = ErrorSpotControl.None;
            int ErrorDiameter             = 0;
            int ErrorSpots                = 0;

            // make sure module size is not zero
            if (QRModuleSize == 0)
            {
                MessageBox.Show("Module size must be defined");
                return;
            }

            // make sure quiet zone is not zero
            if (QRQuietZone < 0)
            {
                MessageBox.Show("Quiet zone must be defined");
                return;
            }


            // image dimension
            QRImageDimension = QRCode.QRCodeImageDimension(QRModuleSize, QRQuietZone);

            // display qr code over image made with a brush
            if (BrushRadioButton.Checked)
            {
                // area width
                if (!int.TryParse(BrushWidthTextBox.Text.Trim(), out ImageWidth) ||
                    ImageWidth <= 0 || ImageWidth >= 100000)
                {
                    MessageBox.Show("Brush background width is invalid");
                    return;
                }

                // area width
                if (!int.TryParse(BrushHeightTextBox.Text.Trim(), out ImageHeight) ||
                    ImageHeight <= 0 || ImageHeight >= 100000)
                {
                    MessageBox.Show("Brush background height is invalid");
                    return;
                }
            }

            // display qr code over an image
            if (ImageRadioButton.Checked)
            {
                // image must be defined
                if (ImageBitmap == null)
                {
                    MessageBox.Show("Background image must be defined");
                    return;
                }

                ImageWidth  = ImageBitmap.Width;
                ImageHeight = ImageBitmap.Height;
            }

            if (!NoneRadioButton.Checked)
            {
                // QR code position X
                if (!int.TryParse(QRCodePosXTextBox.Text.Trim(), out QRCodePosX) || QRCodePosX <= 0 || QRCodePosX >= ImageWidth)
                {
                    MessageBox.Show("QR code position X must be within image width");
                    return;
                }

                // QR code position Y
                if (!int.TryParse(QRCodePosYTextBox.Text.Trim(), out QRCodePosY) || QRCodePosY <= 0 || QRCodePosY >= ImageHeight)
                {
                    MessageBox.Show("QR code position Y must be within image height");
                    return;
                }

                // rotation
                if (!int.TryParse(ImageRotationTextBox.Text.Trim(), out CameraRotation) || CameraRotation < -360 || CameraRotation > 360)
                {
                    MessageBox.Show("Rotation must be -360 to 360");
                    return;
                }

                // camera distance
                if (!int.TryParse(CameraDistanceTextBox.Text.Trim(), out CameraDistance) || CameraDistance < 10 * QRModuleSize)
                {
                    MessageBox.Show("Camera distance is invalid");
                    return;
                }

                // Axis X Rotation
                if (!int.TryParse(CameraViewRotationTextBox.Text.Trim(), out ViewXRotation) || ViewXRotation > 160 || ViewXRotation < -160)
                {
                    MessageBox.Show("View X rotation invalid");
                    return;
                }
            }

            // error
            if (!ErrorNoneRadioButton.Checked)
            {
                if (ErrorWhiteRadioButton.Checked)
                {
                    ErrorControl = ErrorSpotControl.White;
                }
                else if (ErrorBlackRadioButton.Checked)
                {
                    ErrorControl = ErrorSpotControl.Black;
                }
                else
                {
                    ErrorControl = ErrorSpotControl.Alternate;
                }

                int MaxSpotDiameter = QRCode.QRCodeImageDimension(QRModuleSize, QRQuietZone) / 8;
                if (!int.TryParse(ErrorDiameterTextBox.Text.Trim(), out ErrorDiameter) ||
                    ErrorDiameter <= 0 || ErrorDiameter > MaxSpotDiameter)
                {
                    MessageBox.Show("Error diameter is invalid");
                    return;
                }

                if (!int.TryParse(ErrorNumberTextBox.Text.Trim(), out ErrorSpots) ||
                    ErrorSpots <= 0 || ErrorSpots > 100)
                {
                    MessageBox.Show("Number of error spots is invalid");
                    return;
                }
            }

            // get file name
            string FileName = SaveFileName();

            if (FileName == null)
            {
                return;
            }

            // output bitmap
            Bitmap OutputBitmap;

            // display QR Code image by itself
            if (NoneRadioButton.Checked)
            {
                OutputBitmap = QRCodeToBitmap.CreateBitmap(QRCode, QRModuleSize, QRQuietZone);
            }

            else
            {
                if (ImageRadioButton.Checked)
                {
                    OutputBitmap = new Bitmap(ImageBitmap);
                }
                else
                {
                    // create area brush
                    Brush AreaBrush = (int)HatchStyleComboBox.SelectedItem < 0 ? (Brush) new SolidBrush(BrushColorButton.BackColor) :
                                      (Brush) new HatchBrush((HatchStyle)((int)HatchStyleComboBox.SelectedItem), Color.Black, BrushColorButton.BackColor);

                    // create picture object and and paint it with the brush
                    OutputBitmap = new Bitmap(ImageWidth, ImageHeight);
                    Graphics Graphics = Graphics.FromImage(OutputBitmap);
                    Graphics.FillRectangle(AreaBrush, 0, 0, ImageWidth, ImageHeight);
                }

                if (ViewXRotation == 0)
                {
                    OutputBitmap = QRCodeToBitmap.CreateBitmap(QRCode, QRModuleSize, QRQuietZone, OutputBitmap,
                                                               QRCodePosX, QRCodePosY, CameraRotation);
                }
                else
                {
                    OutputBitmap = QRCodeToBitmap.CreateBitmap(QRCode, QRModuleSize, QRQuietZone, OutputBitmap,
                                                               QRCodePosX, QRCodePosY, CameraRotation, CameraDistance, ViewXRotation);
                }
            }

            // Error spots
            if (ErrorControl != ErrorSpotControl.None)
            {
                QRCodeToBitmap.AddErrorSpots(OutputBitmap, ErrorControl, ErrorDiameter, ErrorSpots);
            }

            // save image
            FileStream FS = new FileStream(FileName, FileMode.Create);

            OutputBitmap.Save(FS, (ImageFormat)ImageFormatComboBox.SelectedItem);
            FS.Close();

            // start image editor
            Process.Start(FileName);
            return;
        }
        private void EncodeButton_Click(object sender, EventArgs e)
        {
            // get error correction code
            ErrorCorrection ErrorCorrection = (ErrorCorrection)ErrorCorrectionComboBox.SelectedIndex;

            // get data for QR Code
            StringBuilder b = new StringBuilder();

            if (lsvThietBi.SelectedIndices.Count > 0)
            {
                b.AppendLine(this.label1.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[0].Text);
                b.AppendLine(this.label2.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[1].Text);
                b.AppendLine(this.label3.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[2].Text);
                b.AppendLine(this.label4.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[3].Text);
                b.AppendLine(this.label5.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[4].Text);
                b.AppendLine(this.label6.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[5].Text);
                b.AppendLine(this.label7.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[6].Text);
                b.AppendLine(this.label8.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[7].Text);
                b.AppendLine(this.label9.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[8].Text);
                b.AppendLine(this.label10.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[9].Text);
                b.AppendLine(this.label12.Text + ":" + lsvThietBi.SelectedItems[0].SubItems[10].Text);
            }
            else
            {
                MessageBox.Show("You don't choose data!");
                return;
            }
            string Data = b.ToString();

            // save state
            ProgramState.State.EncodeErrorCorrection = ErrorCorrection;
            ProgramState.State.EncodeData            = Data;
            ProgramState.SaveState();

            // disable buttons
            EnableButtons(false);

            try
            {
                // multi segment
                if (SeparatorCheckBox.Checked && Data.IndexOf('|') >= 0)
                {
                    string[] Segments = Data.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                    // encode data
                    QRCodeEncoder.Encode(ErrorCorrection, Segments);
                }

                // single segment
                else
                {
                    // encode data
                    QRCodeEncoder.Encode(ErrorCorrection, Data);
                }

                // create bitmap
                QRCodeImage = QRCodeToBitmap.CreateBitmap(QRCodeEncoder, 4, 8);
            }

            catch (Exception Ex)
            {
                MessageBox.Show("Encoding exception.\r\n" + Ex.Message);
            }

            // enable buttons
            EnableButtons(true);

            // repaint panel
            Invalidate();
            return;
        }