/// <summary> /// Creates the label value image element. /// </summary> /// <param name="linearEncoder"></param> /// <param name="quietzone"></param> /// <returns>The generated label value image inside an ImageElement object.</returns> internal static ImageElement GetHumanReadableImageCentered(LinearEncoder linearEncoder, int quietzone) { // Create an empty ImageElement ImageElement humanReadableElement = new ImageElement(); // If the human readable position is set to hidden, return the empty ImageElement if (linearEncoder.HumanReadablePosition == HumanReadablePosition.Hidden) { return(humanReadableElement); } // If the human readable is set to a visible position, but has no value, take the barcode value if (string.IsNullOrWhiteSpace(linearEncoder.HumanReadableValue)) { linearEncoder.HumanReadableValue = linearEncoder.EncodedValue; } // Calculate the barcode image width from the minimum width multiplied by the x-dimension int barcodeWidth = linearEncoder.LinearEncoding.MinimumWidth * linearEncoder.XDimension; // Get the original human readable font size, so we can compare with the size after adjusting to fir barcode width int humanReadableFontSizeOriginal = (int)linearEncoder.HumanReadableFont.Size; // Adjust the human readable font size so that the value does not exceed the width of the barcode image linearEncoder.HumanReadableFont = ImageHelpers.GetSizedFontForWidth(linearEncoder.HumanReadableValue, barcodeWidth, linearEncoder.Dpi, linearEncoder.HumanReadableFont); // Set the human readable font size changed flag, if size different from original if (humanReadableFontSizeOriginal != (int)linearEncoder.HumanReadableFont.Size) { linearEncoder.HumanReadableFontSizeChanged = true; } // Measure the value label text size, based on the font provided SizeF labelTextSize = ImageHelpers.GetStringElementSize(linearEncoder.HumanReadableValue, linearEncoder.HumanReadableFont, linearEncoder.Dpi); // Create a new bitmap image for the label value text based on the calculated dimensions humanReadableElement.UpdateImage(new Bitmap((int)Math.Ceiling(labelTextSize.Width), (int)Math.Ceiling(labelTextSize.Height))); humanReadableElement.Image.SetResolution(linearEncoder.Dpi, linearEncoder.Dpi); // Create a new graphics to draw on the barcode image Graphics labelValueGraphics = Graphics.FromImage(humanReadableElement.Image); labelValueGraphics.FillRectangle(Brushes.White, 1, 1, humanReadableElement.Image.Width, humanReadableElement.Image.Height); labelValueGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; labelValueGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; labelValueGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; labelValueGraphics.DrawString(linearEncoder.HumanReadableValue, linearEncoder.HumanReadableFont, Brushes.Black, 1, 1); labelValueGraphics.Flush(); humanReadableElement.Position.XPosition = quietzone + (barcodeWidth - (int)labelTextSize.Width) / 2; return(humanReadableElement); }
/// <summary> /// Creates the encoding text image element. /// </summary> /// <param name="linearEncoder"></param> /// <param name="quietzone"></param> /// <returns>The generated encoding text image inside an ImageElement object.</returns> internal static ImageElement GetEncodingImage(LinearEncoder linearEncoder, int quietzone) { // Create an empty ImageElement ImageElement encodingTextElement = new ImageElement(); // Return the empty ImageElement if encoding not visible if (linearEncoder.ShowEncoding == false) { return(encodingTextElement); } // Setup the font for encoding with initial value "A" to assist calculating encoding element height later. Font encodeCharFont = ImageHelpers.GetSizedFontForWidth(1, linearEncoder.LinearEncoding.GetWidestSymbol() * linearEncoder.XDimension, linearEncoder.Dpi, new Font(linearEncoder.EncodingFontFamily, 8), false); // Set the text size so that we can get the encoding element height later. SizeF encodingTextSize = ImageHelpers.GetStringElementSize("A", encodeCharFont, linearEncoder.Dpi); // Set the encoding image width from the minimum width multiplied by the x-dimension. int encodingImageWidth = linearEncoder.LinearEncoding.MinimumWidth * linearEncoder.XDimension; //Create a new bitmap image for the encoding text based on the calculated dimensions. encodingTextElement.UpdateImage(new Bitmap(encodingImageWidth, (int)Math.Ceiling(encodingTextSize.Height))); encodingTextElement.Image.SetResolution(linearEncoder.Dpi, linearEncoder.Dpi); // Create a new graphics to draw on the encoded text image. Graphics encodingTextGraphics = Graphics.FromImage(encodingTextElement.Image); encodingTextGraphics.FillRectangle(Brushes.White, 0, 0, encodingImageWidth, (int)Math.Ceiling(encodingTextSize.Height)); int xPosition = 0; int yPosition = 0; Pen encodePen = new Pen(Brushes.Black, 1); for (int symbol = 0; symbol <= linearEncoder.LinearEncoding.Symbols.Count - 1; symbol++) { string encodeCharacter = linearEncoder.LinearEncoding.Symbols[symbol].Character; encodeCharFont = ImageHelpers.GetSizedFontForWidth(encodeCharacter.Length, linearEncoder.LinearEncoding.Symbols[symbol].Width * linearEncoder.XDimension, linearEncoder.Dpi, new Font(linearEncoder.EncodingFontFamily, 8), false); int symbolWidth = linearEncoder.LinearEncoding.Symbols[symbol].Width; //string SymbolPattern = coded.SymbolPattern[symbol]; Brush encodeBrush; if (linearEncoder.LinearEncoding.Symbols[symbol].CharacterType == 1) { encodingTextGraphics.FillRectangle(Brushes.Black, xPosition, yPosition, symbolWidth * linearEncoder.XDimension, encodingTextElement.Image.Height); encodeBrush = Brushes.White; } else { encodingTextGraphics.DrawRectangle(encodePen, xPosition, yPosition, (symbolWidth * linearEncoder.XDimension) - 1, encodingTextElement.Image.Height - 1); encodeBrush = Brushes.Black; } encodingTextGraphics.DrawString(encodeCharacter, encodeCharFont, encodeBrush, xPosition, yPosition); xPosition += symbolWidth * linearEncoder.XDimension; } encodeCharFont.Dispose(); encodingTextGraphics.Dispose(); encodePen.Dispose(); encodingTextElement.Position.XPosition = quietzone; return(encodingTextElement); }
/// <summary> /// Creates the label value image element. /// </summary> /// <param name="linearEncoder"></param> /// <param name="quietzone"></param> /// <returns>The generated label value image inside an ImageElement object.</returns> internal static ImageElement GetHumanReadableImageSymbolAligned(LinearEncoder linearEncoder, int quietzone) { // Create an empty ImageElement ImageElement humanReadableElement = new ImageElement(); // If the human readable position is set to hidden, return the empty ImageElement if (linearEncoder.HumanReadablePosition == HumanReadablePosition.Hidden) { return(humanReadableElement); } // Setup the label font with initial value "A" to assist calculating label element height later Font humanReadableFont = ImageHelpers.GetSizedFontForWidth(1, linearEncoder.LinearEncoding.GetWidestSymbol() * linearEncoder.XDimension, linearEncoder.Dpi, linearEncoder.HumanReadableFont); // Set the text size so that we can get the encoding element height later SizeF humanReadableSize = ImageHelpers.GetStringElementSize("W", humanReadableFont, linearEncoder.Dpi); int prefixWidth = linearEncoder.LinearEncoding.HumanReadablePrefix?.Length * (int)humanReadableSize.Width ?? 0; int suffixWidth = linearEncoder.LinearEncoding.HumanReadableSuffix?.Length * (int)humanReadableSize.Width ?? 0; // Set the encoding image width from the minimum width multiplied by the x-dimension int humanReadableImageWidth = linearEncoder.LinearEncoding.MinimumWidth * linearEncoder.XDimension + prefixWidth + suffixWidth; //Create a new bitmap image for the encoding text based on the calculated dimensions humanReadableElement.UpdateImage(new Bitmap(humanReadableImageWidth, (int)Math.Ceiling(humanReadableSize.Height))); humanReadableElement.Image.SetResolution(linearEncoder.Dpi, linearEncoder.Dpi); // Create a new graphics to draw on the encoded text image Graphics humanReadableGraphics = Graphics.FromImage(humanReadableElement.Image); int xPosition = 0; int yPosition = 0; StringFormat humanReadableFormat = new StringFormat { Alignment = StringAlignment.Center }; RectangleF humanReadableRectangle; // Add any human readable prefix if (linearEncoder.LinearEncoding.HumanReadablePrefix != null) { humanReadableRectangle = new RectangleF(xPosition, yPosition, prefixWidth, humanReadableElement.Image.Height); humanReadableGraphics.FillRectangle(Brushes.White, humanReadableRectangle); humanReadableGraphics.DrawString(linearEncoder.LinearEncoding.HumanReadablePrefix, humanReadableFont, Brushes.Black, humanReadableRectangle, humanReadableFormat); xPosition += prefixWidth; } for (int symbol = 0; symbol <= linearEncoder.LinearEncoding.Symbols.Count - 1; symbol++) { string humanReadableCharacter = linearEncoder.LinearEncoding.Symbols[symbol].Character; Font humanReadableCharacterFont = ImageHelpers.GetSizedFontForWidth(humanReadableCharacter.Length, linearEncoder.LinearEncoding.Symbols[symbol].Width * linearEncoder.XDimension, linearEncoder.Dpi, linearEncoder.HumanReadableFont); int symbolWidth = linearEncoder.LinearEncoding.Symbols[symbol].Width; if (linearEncoder.LinearEncoding.Symbols[symbol].CharacterType == 0) { humanReadableRectangle = new RectangleF(xPosition, yPosition, symbolWidth * linearEncoder.XDimension, humanReadableElement.Image.Height); humanReadableGraphics.FillRectangle(Brushes.White, humanReadableRectangle); humanReadableGraphics.DrawString(humanReadableCharacter, humanReadableCharacterFont, Brushes.Black, humanReadableRectangle, humanReadableFormat); } xPosition += symbolWidth * linearEncoder.XDimension; } // Add any human readable suffix if (linearEncoder.LinearEncoding.HumanReadableSuffix != null) { humanReadableRectangle = new RectangleF(xPosition, yPosition, suffixWidth, humanReadableElement.Image.Height); humanReadableGraphics.FillRectangle(Brushes.White, humanReadableRectangle); humanReadableGraphics.DrawString(linearEncoder.LinearEncoding.HumanReadableSuffix, humanReadableFont, Brushes.Black, humanReadableRectangle, humanReadableFormat); } humanReadableFont.Dispose(); humanReadableGraphics.Dispose(); humanReadableElement.Position.XPosition = quietzone - prefixWidth; return(humanReadableElement); }