/// <summary> /// Add error spots for testing /// </summary> /// <param name="ImageBitmap">Image Bitmap</param> /// <param name="ErrorControl">Error control enumeration</param> /// <param name="ErrorDiameter">Error spot diameter</param> /// <param name="ErrorSpotsCount">Error spots count</param> public static void AddErrorSpots ( Bitmap ImageBitmap, ErrorSpotControl ErrorControl, double ErrorDiameter, double ErrorSpotsCount ) { // random number generator Random RandNum = new Random(); // create graphics object Graphics Graphics = Graphics.FromImage(ImageBitmap); double XRange = ImageBitmap.Width - ErrorDiameter - 4; double YRange = ImageBitmap.Height - ErrorDiameter - 4; SolidBrush SpotBrush = ErrorControl == ErrorSpotControl.Black ? BrushBlack : BrushWhite; for (int Index = 0; Index < ErrorSpotsCount; Index++) { double XPos = RandNum.NextDouble() * XRange; double YPos = RandNum.NextDouble() * YRange; if (ErrorControl == ErrorSpotControl.Alternate) { SpotBrush = (Index & 1) == 0 ? BrushWhite : BrushBlack; } Graphics.FillEllipse(SpotBrush, (float)XPos, (float)YPos, (float)ErrorDiameter, (float)ErrorDiameter); } return; }
/// <summary> /// Добавление областей ошибок для тестирования /// </summary> /// <param name="ImageBitmap">Растровое изображение</param> /// <param name="ErrorControl">Перечисление контроля ошибок</param> /// <param name="ErrorDiameter">Диаметр пятна ошибки</param> /// <param name="ErrorSpotsCount">Количество пятен ошибок</param> public static void AddErrorSpots(Bitmap ImageBitmap, ErrorSpotControl ErrorControl, Double ErrorDiameter, Double ErrorSpotsCount) { // Генератор случайных чисел Random RandNum = new Random(); // Создание графического объекта Graphics Graphics = Graphics.FromImage(ImageBitmap); Double XRange = ImageBitmap.Width - ErrorDiameter - 4; Double YRange = ImageBitmap.Height - ErrorDiameter - 4; SolidBrush SpotBrush = ErrorControl == ErrorSpotControl.Black ? BrushBlack : BrushWhite; for (int Index = 0; Index < ErrorSpotsCount; Index++) { Double XPos = RandNum.NextDouble() * XRange; Double YPos = RandNum.NextDouble() * YRange; if (ErrorControl == ErrorSpotControl.Alternate) SpotBrush = (Index & 1) == 0 ? BrushWhite : BrushBlack; Graphics.FillEllipse(SpotBrush, (float) XPos, (float) YPos, (float) ErrorDiameter, (float) ErrorDiameter); } return; }
private void OnSaveClick(object sender, EventArgs e) { // reset some variables int CameraDistance = 0; int CameraRotation = 0; int OutputImageWidth = 0; int OutputImageHeight = 0; int QRCodePosX = 0; int QRCodePosY = 0; int ViewXRotation = 0; ErrorSpotControl ErrorControl = ErrorSpotControl.None; int ErrorDiameter = 0; int ErrorSpots = 0; // display qr code over image made with a brush if (BrushRadioButton.Checked) { // area width if (!int.TryParse(BrushWidthTextBox.Text.Trim(), out OutputImageWidth) || OutputImageWidth <= 0 || OutputImageWidth >= 100000) { MessageBox.Show("Brush background width is invalid"); return; } // area width if (!int.TryParse(BrushHeightTextBox.Text.Trim(), out OutputImageHeight) || OutputImageHeight <= 0 || OutputImageHeight >= 100000) { MessageBox.Show("Brush background height is invalid"); return; } } // display qr code over an image if (ImageRadioButton.Checked) { // image must be defined if (this.BGImageBitmap == null) { MessageBox.Show("Background image must be defined"); return; } OutputImageWidth = this.BGImageBitmap.Width; OutputImageHeight = this.BGImageBitmap.Height; } if (!NoneRadioButton.Checked) { // QR code position X if (!int.TryParse(QRCodePosXTextBox.Text.Trim(), out QRCodePosX) || QRCodePosX <= 0 || QRCodePosX >= OutputImageWidth) { 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 >= OutputImageHeight) { 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 * Encoder.ModuleSize) { 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 = Encoder.QRCodeImageDimension / 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 = QRCodeBitmapImage; // QREncoder.CreateQRCodeBitmap(); } else { if (ImageRadioButton.Checked) { OutputBitmap = new Bitmap(this.BGImageBitmap); } 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(OutputImageWidth, OutputImageHeight); Graphics Graphics = Graphics.FromImage(OutputBitmap); Graphics.FillRectangle(AreaBrush, 0, 0, OutputImageWidth, OutputImageHeight); } if (ViewXRotation == 0) { OutputBitmap = CreateQRCodeImage(OutputBitmap, QRCodePosX, QRCodePosY, CameraRotation); } else { OutputBitmap = CreateQRCodeImage(OutputBitmap, QRCodePosX, QRCodePosY, CameraRotation, CameraDistance, ViewXRotation); } } // Error spots if (ErrorControl != ErrorSpotControl.None) { 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; }