/// <summary> /// Creates a new instance of the <see cref="ImageBuilder"/> class. /// </summary> public ImageBuilder() { _drawFormat = new StringFormat(); _drawFormat.Alignment = StringAlignment.Center; _unit = BarCodeUnit.Inch; _dpi = UnitConverter.ScreenDpi; }
/// <summary> /// Converts and stores the settings' convertible properties to another unit of measure. /// </summary> /// <param name="fromUnit">The unit the settings properties are.</param> /// <param name="toUnit">the unit to convert to.</param> public void ConvertValues(BarCodeUnit fromUnit, BarCodeUnit toUnit) { int dpi = _settings.Dpi; _settings.BarHeight = UnitConverter.Convert(_settings.BarHeight, fromUnit, toUnit, dpi); _settings.GuardExtraHeight = UnitConverter.Convert(_settings.GuardExtraHeight, fromUnit, toUnit, dpi); _settings.ModuleWidth = UnitConverter.Convert(_settings.ModuleWidth, fromUnit, toUnit, dpi); _settings.NarrowWidth = UnitConverter.Convert(_settings.NarrowWidth, fromUnit, toUnit, dpi); _settings.WideWidth = UnitConverter.Convert(_settings.WideWidth, fromUnit, toUnit, dpi); _settings.OffsetHeight = UnitConverter.Convert(_settings.OffsetHeight, fromUnit, toUnit, dpi); _settings.OffsetWidth = UnitConverter.Convert(_settings.OffsetWidth, fromUnit, toUnit, dpi); }
/// <summary> /// Convert a value to another DPI. /// If the value is in a non-pixel unit, the same value is returned to maintain the aspect ratio. /// </summary> /// <param name="value">Value to convert.</param> /// <param name="unit">Value unit.</param> /// <param name="fromDpi">The current DPI of the value.</param> /// <param name="toDpi">The target DPI for conversion.</param> /// <returns>The value in the new DPI.</returns> public static float ConvertDpi(float value, BarCodeUnit unit, int fromDpi, int toDpi) { if (fromDpi <= 0 || toDpi <= 0) { throw new ArgumentException("DPIs must be greater than zero."); } if (unit != BarCodeUnit.Pixel) { return(value); } var ratio = toDpi / (float)fromDpi; return(value * ratio); }
/// <summary> /// Converts a value between measuring units. /// </summary> /// <param name="value">Value to be converted.</param> /// <param name="sourceUnit">The unit to convert from.</param> /// <param name="targetUnit">The unit to convert to.</param> /// <param name="dpi">The DPI to use.</param> /// <returns>The converted unit.</returns> public static float Convert(float value, BarCodeUnit sourceUnit, BarCodeUnit targetUnit, int dpi) { if (dpi <= 0) { throw new ArgumentException("DPI must be greater than zero."); } if (sourceUnit == targetUnit) { return(value); } // keep track of the value being converted float converted = value; int sourceUnitIndex = (int)sourceUnit; if (sourceUnit == BarCodeUnit.Pixel) { // convert the value from pixels to inches converted /= dpi; sourceUnitIndex = (int)BarCodeUnit.Inch; } int targetUnitIndex = (int)targetUnit; if (targetUnit == BarCodeUnit.Pixel) { // apply the inch to pixel ratio (the DPI) converted *= dpi; // convert to inches rather than to pixels targetUnitIndex = (int)BarCodeUnit.Inch; } converted *= ConversionTable [sourceUnitIndex, targetUnitIndex]; return(converted); }