private bool CalculateSize(ref Size currentSize) { IBarcodeEncoder enc = GetEncoder(this.BarcodeType); if (enc == null) { throw new Exception("That encoder is not implemented yet."); } IBarcodeSizer sizer = enc.Sizer; IBarcodeModularSizer mSizer = sizer as IBarcodeModularSizer; try { enc.Text = this.Data; sizer = enc.Sizer; mSizer = sizer as IBarcodeModularSizer; sizer.DPI = (float)this.Dpi.Value; } catch (ArgumentException ex) { throw ex; } BarcodeRenderMode mode = BarcodeRenderMode.None; if (this.Guarded) { mode |= BarcodeRenderMode.Guarded; } if (this.Notched) { mode |= BarcodeRenderMode.Notched; } if (this.Numbered) { mode |= BarcodeRenderMode.Numbered; } if (this.Braced) { mode |= BarcodeRenderMode.Braced; } if (this.Boxed) { mode |= BarcodeRenderMode.Boxed; } sizer.Mode = mode; if (this.Height.Value < sizer.Height) { this.Height = new Unit(sizer.Height); } Size actualSize; if (sizer.DPI == 0 || mSizer == null) { //Logical mode, or simple fixed size. actualSize = sizer.GetValidSize(new Size((int)sizer.Width, (int)Height.Value)); SetSizeDisplay(actualSize, ref currentSize); } else { actualSize = sizer.GetValidSize(new Size((int)sizer.Width, (int)Height.Value)); SetSizeDisplay(actualSize, ref currentSize); } return(true); }
private bool CalculateSize() { IBarcodeEncoder enc = GetEncoder(); if (enc == null) { return(false); } IBarcodeSizer sizer = enc.Sizer; IBarcodeModularSizer mSizer = sizer as IBarcodeModularSizer; try { //if ((enc.Flags&BarcodeEncoderFlags.Composite)!=0 || // (enc.Generator!=null && (enc.Generator.Flags & BarcodeGeneratorFlags.VariableLength)!=0)) enc.Text = _tbData.Text; //if ((enc.Flags & BarcodeEncoderFlags.Composite) != 0) { sizer = enc.Sizer; mSizer = sizer as IBarcodeModularSizer; //} sizer.DPI = (int)_nupDpi.Value; } catch (ArgumentException ex) { MessageBox.Show(ex.Message); return(false); } BarcodeRenderMode mode = BarcodeRenderMode.None; if (_cbGuarded.Checked) { mode |= BarcodeRenderMode.Guarded; } if (_cbNotched.Checked) { mode |= BarcodeRenderMode.Notched; } if (_cbNumbered.Checked) { mode |= BarcodeRenderMode.Numbered; } if (_cbBraced.Checked) { mode |= BarcodeRenderMode.Braced; } if (_cbBoxed.Checked) { mode |= BarcodeRenderMode.Boxed; } sizer.Mode = mode; //Ok, now what we do depends on the options that are set. if (_nudHeight.Value < sizer.Height) { _nudHeight.Value = sizer.Height; } Size actualSize; if (sizer.DPI == 0 || mSizer == null) { //Logical mode, or simple fixed size. actualSize = sizer.GetValidSize(new Size(sizer.Width, (int)_nudHeight.Value)); SetSizeDisplay(actualSize); } else if (_rbDesiredWidth.Checked) { //They have given a desired width. if (_nudDesiredWidth.Value == 0) { actualSize = sizer.GetValidSize(new Size(sizer.Width, (int)_nudHeight.Value)); SetSizeDisplay(actualSize); } else { float mils = BarcodeUtilities.CalculateModuleWidth(mSizer, (int)(mSizer.DPI * (float)_nudDesiredWidth.Value)); _nudModuleWidth.Value = (decimal)mils; mSizer.Module = mils; if (_nudHeight.Value < sizer.Height) { _nudHeight.Value = sizer.Height; } actualSize = sizer.GetValidSize(new Size(sizer.Width, (int)_nudHeight.Value)); SetSizeDisplay(actualSize); } } else { //rbModule checked. mSizer.Module = (float)_nudModuleWidth.Value; if (_nudHeight.Value < sizer.Height) { _nudHeight.Value = sizer.Height; } actualSize = sizer.GetValidSize(new Size(sizer.Width, (int)_nudHeight.Value)); SetSizeDisplay(actualSize); } return(true); }
private bool GenerateBarecode() { // --- STANDARD --- if (_rbStandard.Checked) { IBarcodeEncoder enc = GetEncoder(); if (enc == null) { MessageBox.Show("That encoder is not implemented yet."); return(false); } try { enc.Text = _tbData.Text; } catch (ArgumentException ex) { MessageBox.Show(ex.Message); return(false); } BarcodeRenderMode mode = BarcodeRenderMode.None; if (_cbGuarded.Checked) { mode |= BarcodeRenderMode.Guarded; } if (_cbNotched.Checked) { mode |= BarcodeRenderMode.Notched; } if (_cbNumbered.Checked) { mode |= BarcodeRenderMode.Numbered; } if (_cbBraced.Checked) { mode |= BarcodeRenderMode.Braced; } if (_cbBoxed.Checked) { mode |= BarcodeRenderMode.Boxed; } enc.Sizer.Mode = mode; enc.Sizer.DPI = (int)_nupDpi.Value; IBarcodeModularSizer mSizer = enc.Sizer as IBarcodeModularSizer; if (mSizer != null) { if (_nudModuleWidth.Value != 0) { mSizer.Module = (float)_nudModuleWidth.Value; } } bool calculResult = CalculateSize(); if (calculResult == false) { return(false); } if (!enc.Sizer.IsValidSize(_currentSize)) { MessageBox.Show("Invalid size."); return(false); } IBarcodeGenerator gen = enc.Generator; _bmpPrimary = gen.GenerateBarcode(_currentSize); _pbBarcode.Image = _bmpPrimary; } // --- HIBC --- else { ActiveUp.WebControls.SecondaryDataHibc secondData = new ActiveUp.WebControls.SecondaryDataHibc(); if (_tbDate.Text != "") { try { secondData.Date = DateTime.Parse(_tbDate.Text); } catch (Exception) { MessageBox.Show("Invalid date format in the Date box."); return(false); } } if (_tbQuantity.Text != "") { try { secondData.Quantity = int.Parse(_tbQuantity.Text); } catch (Exception) { MessageBox.Show("The value in the quantity box must be a number between 0 and 99999."); return(false); } } if (_tbSerial.Text != "") { secondData.Lot = _tbSerial.Text; } if (_comboDateFormat.SelectedItem != null) { try { secondData.DateFormat = (ActiveUp.WebControls.HibcDateFormat)Enum.Parse(typeof(ActiveUp.WebControls.HibcDateFormat), (string)_comboDateFormat.SelectedItem); } catch (Exception) { MessageBox.Show("An error occured in parsing the date format."); return(false); } } Regex licCheck = new Regex("[A-Z][A-Z0-9]{3}", RegexOptions.IgnoreCase); Regex pcnCheck = new Regex("[A-Z0-9]{1,13}", RegexOptions.IgnoreCase); Regex gs1Check = new Regex("[0-9]{12}"); if (_rbHIBCLIC.Checked) { //Check the LIC and PCN. if (_tbLICorGS1.Text == "" || !licCheck.IsMatch(_tbLICorGS1.Text)) { MessageBox.Show("The LIC is not formatted correctly."); return(false); } if (_tbPCN.Text != "" && !pcnCheck.IsMatch(_tbPCN.Text)) { MessageBox.Show("The PCN is not formatted correctly."); return(false); } } else { //GS1 if (_tbLICorGS1.Text == "" || !gs1Check.IsMatch(_tbLICorGS1.Text)) { MessageBox.Show("The GS1 is not formatted correctly."); return(false); } if (_tbPCN.Text != "" && !pcnCheck.IsMatch(_tbPCN.Text)) { MessageBox.Show("The PCN is not formatted correctly."); return(false); } } byte uom; try { uom = byte.Parse((string)_comboUnitOfMeasurement.SelectedItem); } catch (Exception) { MessageBox.Show("The Unit of Measurement is not set."); return(false); } IBarcodeEncoder[] encoders; ActiveUp.WebControls.PrimaryEncodingMode pMode; ActiveUp.WebControls.SecondaryEncodingMode sMode; try { pMode = (ActiveUp.WebControls.PrimaryEncodingMode)Enum.Parse(typeof(ActiveUp.WebControls.PrimaryEncodingMode), (string)_comboPrimaryMode.SelectedItem); } catch (Exception) { MessageBox.Show("An error occured in parsing the primary encoding mode."); return(false); } try { sMode = (ActiveUp.WebControls.SecondaryEncodingMode)Enum.Parse(typeof(ActiveUp.WebControls.SecondaryEncodingMode), (string)_comboSecondaryMode.SelectedItem); } catch (Exception) { MessageBox.Show("An error occured in parsing the secondary encoding mode."); return(false); } try { if (_rbHIBCLIC.Checked) { encoders = ActiveUp.WebControls.HIBCEncoder.EncodeHIBC(_tbLICorGS1.Text, _tbPCN.Text, uom, secondData, pMode, sMode); } else { encoders = ActiveUp.WebControls.HIBCEncoder.EncodeGS1(uom.ToString() + _tbLICorGS1.Text, (_tbPCN.Text == "") ? null : _tbPCN.Text, secondData, pMode, sMode); } } catch (Exception ex) { MessageBox.Show("An exception occured:\n" + ex.Message); return(false); } encoders[0].Sizer.DPI = (float)_nupDpi.Value; if (encoders.Length == 2) { encoders[1].Sizer.DPI = (float)_nupDpi.Value; } encoders[0].Sizer.Mode = BarcodeRenderMode.Guarded | BarcodeRenderMode.Numbered; if (encoders.Length == 2) { encoders[1].Sizer.Mode = BarcodeRenderMode.Guarded | BarcodeRenderMode.Numbered; } _bmpPrimary = encoders[0].Generator.GenerateBarcode(encoders[0].Sizer.Size); if (encoders.Length == 2) { _bmpSecondary = encoders[1].Generator.GenerateBarcode(encoders[1].Sizer.Size); } if (_rbPrimary.Checked) { _pbBarcode.Image = _bmpPrimary; } else { _pbBarcode.Image = _bmpSecondary; } } return(true); }
/// <summary> /// Calculates the module width for a given barcode, with a default tolerance. /// </summary> /// <remarks> /// This routine is the equivilent to a call of /// <c><see cref="Barcodes.BarcodeUtilities.CalculateModuleWidth(Barcodes.IBarcodeModularSizer,int, bool, float)"/>(<paramref name="sizer"/>, <paramref name="width"/>, <paramref name="aimHigh"/>, .1f)</c>. /// </remarks> /// <param name="sizer">The sizer instance for the barcode to be sized.</param> /// <param name="width">The desired pixel width.</param> /// <param name="aimHigh">True to return the module width that produces a width as close as possible /// to but not less than the specified width, false to return a module width as close as possible but /// not greater than the specified width.</param> /// <returns>A module width, which produces a barcode as close to the target as possible, but no larger or smaller, depending on <paramref name="aimHigh"/>.</returns> /// <exception cref="System.ArgumentException">The specified pixel width <paramref name="width"/> is smaller than the smallest /// possible width, as calculated with a module width of <see cref="Barcodes.IBarcodeModularSizer.MinimumModule"/>.</exception> /// <exception cref="System.NullReferenceException">The specified <paramref name="sizer"/> is null.</exception> /// <exception cref="System.InvalidOperationException">The <see cref="Barcodes.IBarcodeSizer.DPI"/> is set to zero.</exception> public static float CalculateModuleWidth(IBarcodeModularSizer sizer, int width, bool aimHigh) { return(CalculateModuleWidth(sizer, width, aimHigh, .1f)); }
/// <summary> /// Calculates the module width for a given barcode, with a default tolerance and approach direction. /// </summary> /// <remarks> /// This routine is the equivilent to a call of /// <c><see cref="Barcodes.BarcodeUtilities.CalculateModuleWidth(Barcodes.IBarcodeModularSizer,int, bool, float)"/>(<paramref name="sizer"/>, <paramref name="width"/>, false, .1f)</c>. /// It returns a module width that produces a barcode that is as close to the target as possible, but no larger.</remarks> /// <param name="sizer">The sizer instance for the barcode to be sized.</param> /// <param name="width">The desired pixel width.</param> /// <returns>A module width which produces a barcode as close to the target as possible, but no larger.</returns> /// <exception cref="System.ArgumentException">The specified pixel width <paramref name="width"/> is smaller than the smallest /// possible width, as calculated with a module width of <see cref="Barcodes.IBarcodeModularSizer.MinimumModule"/>.</exception> /// <exception cref="System.NullReferenceException">The specified <paramref name="sizer"/> is null.</exception> /// <exception cref="System.InvalidOperationException">The <see cref="Barcodes.IBarcodeSizer.DPI"/> is set to zero.</exception> public static float CalculateModuleWidth(IBarcodeModularSizer sizer, int width) { return(CalculateModuleWidth(sizer, width, false, .1f)); }
/// <summary> /// Calculates the module width for a given barcode. /// </summary> /// <param name="sizer">The sizer instance for the barcode to be sized.</param> /// <param name="width">The desired pixel width.</param> /// <param name="aimHigh">True to return the module width that produces a width as close as possible /// to but not less than the specified width, false to return a module width as close as possible but /// not greater than the specified width.</param> /// <param name="tolerance">The desired module tolerance; once calculations narrow the module rande /// to a distance lower than this, calculations stop.</param> /// <returns>A module width, which produces a barcode as close to the target as possible, but no larger /// or smaller, depending on <paramref name="aimHigh"/>.</returns> /// <exception cref="System.ArgumentException">The specified pixel width <paramref name="width"/> is smaller than the smallest /// possible width, as calculated with a module width of <see cref="Barcodes.IBarcodeModularSizer.MinimumModule"/>, or /// <paramref name="tolerance"/> is less than <c>0.01</c>.</exception> /// <exception cref="System.NullReferenceException">The specified <paramref name="sizer"/> is null.</exception> /// <exception cref="System.InvalidOperationException">The <see cref="Barcodes.IBarcodeSizer.DPI"/> is set to zero.</exception> public static float CalculateModuleWidth(IBarcodeModularSizer sizer, int width, bool aimHigh, float tolerance) { if (sizer == null) { throw new NullReferenceException("The specified sizer must not be null."); } if (sizer.DPI == 0) { throw new InvalidOperationException("The DPI must be set (non-zero) in order to calculate the module width."); } if (tolerance < .01) { throw new ArgumentException("The specified tolerance must be greater than or equal to 0.01."); } float currentModule = sizer.Module; try { //We do a binary search, looking for a module width that gives the desired width. float currentLow = sizer.MinimumModule; float currentHigh = sizer.MinimumModule; sizer.Module = currentLow; if (width < sizer.Width) { throw new ArgumentException("The specified width is unattainable; it requires a module width below MinimumModule."); } //Find two bracketing module widths, one lower and one higher than the target width. //To ensure that this doesn't take long, it starts ad MinimumModule, and doubles the //high estimate each time. do { currentLow = currentHigh; currentHigh *= 2; sizer.Module = currentHigh; } while (width > sizer.Width); //Ok, the desired module width is between currentLow and currentHigh. //Do a binary search to narrow down the range. float currentMiddle; while ((currentHigh - currentLow) > tolerance) { currentMiddle = (currentLow + currentHigh) / 2; sizer.Module = currentMiddle; if (width < sizer.Width) { currentHigh = currentMiddle; } else if (width > sizer.Width) { currentLow = currentMiddle; } else { return(currentMiddle); //Width==sizer.Width. We found the perfect size. } } //This will be close to the desired size, but will be slightly larger or smaller than the specified width. return(aimHigh?currentHigh:currentLow); } finally { //Make sure the module is reset to its previous value. sizer.Module = currentModule; } }