/// <summary> /// Generates a Standard (Industrial) 2 of 5 barcode of a specified size, using the data that has been set previously by its corresponding /// encoder. /// </summary> /// <param name="size">The size of the barcode to return.</param> /// <returns>A bitmap of the barcode, of the specified size.</returns> /// <exception cref="System.ArgumentException">The specified size is invalid.</exception> /// <exception cref="System.InvalidOperationException">The data that is to be encoded has not been set yet.</exception> public override Bitmap GenerateBarcode(Size size) { if (data == null) { throw new InvalidOperationException("The data for the generator has not been set."); } if (!Sizer.IsValidSize(size)) { throw new ArgumentException("The specified size is not valid."); } EncodeData(); Bitmap bm = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppRgb); Graphics g = Graphics.FromImage(bm); int module, guard; module = ((S2of5Sizer)Sizer).ModuleWidth; guard = ((S2of5Sizer)Sizer).GuardWidth; S2of5Sizer sSizer = (S2of5Sizer)Sizer; if (sSizer.TopWidth != 0) { g.FillRectangle(Brushes.White, Rectangle.FromLTRB(sSizer.SideBrace, sSizer.TopWidth, size.Width - sSizer.SideBrace, size.Height - sSizer.BottomWidth)); } else { g.FillRectangle(Brushes.White, 0, 0, size.Width, size.Height); } int pos = guard; for (int i = 0; i < encodedData.Length; i++) { if (encodedData[i]) { g.FillRectangle(Brushes.Black, pos, 0, module, size.Height); } pos += module; } if (sSizer.FontHeight != 0) { string text = ""; foreach (byte b in data) { text += b.ToString(); } g.FillRectangle(Brushes.White, Rectangle.FromLTRB(0, size.Height - sSizer.FontHeightAddon, size.Width, size.Height)); g.DrawString(text, FontHolder.GenerateFont(sSizer.FontHeight), Brushes.Black, Rectangle.FromLTRB(0, size.Height - sSizer.FontHeightAddon, size.Width, size.Height), FontHolder.CenterJustify); } g.Dispose(); return(bm); }
/// <summary> /// Generates a Codabar barcode of a specified size, using the data that has been set previously by its corresponding /// encoder. /// </summary> /// <param name="size">The size of the barcode to return.</param> /// <returns>A bitmap of the barcode, of the specified size.</returns> /// <exception cref="System.ArgumentException">The specified size is invalid.</exception> /// <exception cref="System.InvalidOperationException">The data that is to be encoded has not been set yet.</exception> public override Bitmap GenerateBarcode(Size size) { if (data == null) { throw new InvalidOperationException("The data for the generator has not been set."); } if (!Sizer.IsValidSize(size)) { throw new ArgumentException("The specified size is not valid."); } EncodeData(); Bitmap bm = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppRgb); Graphics g = Graphics.FromImage(bm); g.FillRectangle(Brushes.White, 0, 0, size.Width, size.Height); int module, guard; module = ((CodabarSizer)Sizer).ModuleWidth; guard = ((CodabarSizer)Sizer).GuardWidth; int pos = guard; for (int i = 0; i < encodedData.Length; i++) { if (encodedData[i]) { g.FillRectangle(Brushes.Black, pos, 0, module, size.Height); } pos += module; } if (((CodabarSizer)Sizer).FontHeight != 0) { string text = ""; foreach (byte b in data) { text += "0123456789-$:/.+ABCD"[b]; } Rectangle textArea = Rectangle.FromLTRB(0, size.Height - ((CodabarSizer)Sizer).FontHeightAddon, size.Width, size.Height); g.FillRectangle(Brushes.White, textArea); g.DrawString(text, FontHolder.GenerateFont(((CodabarSizer)Sizer).FontHeight), Brushes.Black, textArea, FontHolder.CenterJustify); } g.Dispose(); return(bm); }
/// <summary> /// Generates a bitmap of the barcode, of the specified size and data. /// </summary> /// <param name="size">The size of barcode to generate.</param> /// <returns>A bitmap of the generated barcode.</returns> /// <remarks>As the dimensions of the Postnet/Planet barcode are strictly defined, the only valid size is /// the size returned by <see cref="Barcodes.BarcodeSizer.Size"/>.</remarks> /// <exception cref="System.ArgumentException">The specified size is invalid.</exception> /// <exception cref="System.InvalidOperationException">The data that is to be encoded has not been set yet.</exception> public override Bitmap GenerateBarcode(Size size) { if (data == null) { throw new InvalidOperationException("The data to encode has not been set."); } if (!Sizer.IsValidSize(size)) { throw new ArgumentException("The specified size is invalid."); } EncodeData(); PostnetSizer postnetSizer = (PostnetSizer)Sizer; int longBar = postnetSizer.LongBarHeight; int shortBar = postnetSizer.ShortBarHeight; int barWidth = postnetSizer.BarWidth; int moduleWidth = barWidth + postnetSizer.GapWidth; int bHeight = size.Height; Bitmap bm = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppRgb); Graphics g = Graphics.FromImage(bm); g.FillRectangle(Brushes.White, 0, 0, bm.Width, bm.Height); int i; for (i = 0; i < encodedData.Length; i++) { int length = (encodedData[i] ^ isPlanet) ? longBar : shortBar; //PLANET exchanges short and long bars. if (i == 0 || i == encodedData.Length - 1) { length = longBar; //The guard bars are always long. } g.FillRectangle(Brushes.Black, moduleWidth * i, bHeight - length, barWidth, length); } g.Dispose(); return(bm); }