![]() You may need to make adjustments to the template over time to make sure your barcode is the size that you need. Sometimes zebra printers may recalibrate which can affect the scaling. The images that I render are converted to Base64 strings so that they can be embedded directly into HTML img sources.Note: Barcodes for zebra printers may not scale automatically to the height and width values that you set. ![]() Graphics.DrawString(text, font, blackBrush, rectf, strFormat) Graphics.FillRectangle(whiteBrush, rectf) Var whiteBrush = new SolidBrush(Color.White) Var blackBrush = new SolidBrush(Color.Black) StrFormat.LineAlignment = StringAlignment.Center StrFormat.Alignment = StringAlignment.Far Var strFormat = StringFormat.GenericTypographic Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic Using (Graphics graphics = Graphics.FromImage(image)) You can see below that I’m setting the quality of rendering, the alignment (which winds up right-justifying my text), and then using DrawString to render the text. Once the canvas is configured, we need the Graphic object that will actually do all of the work. Var rectf = new RectangleF(0, 0, width, height) Var text = item?.NumericString?.Trim() ? "Unknown" I make the assumption that each character will be roughly 22 pixels wide, and thus calculate the appropriate width of the Bitmap/canvas onto which the barcode string will be drawn. Once I have the font loaded, I’m iterating a list of “items” that contain a numeric string. Your text will wind up looking like early era C-64 text in that case. ![]() If you don’t do this, then the GDI libraries will default to using the system’s DPI settings (usually 96 DPI). Pay special attention to my Font constructor and note that I am specify that units are pixels. Var font = new Font(fontFamily, 44, FontStyle.Regular, GraphicsUnit.Pixel) PrivateFontCollection pfcoll = new PrivateFontCollection() Generate images for our barcodes since ABDPdf doesn't support font-face css In order to utilize an external font file, we must create a private font collection, load the file into the collection, specify the family we want to use, and then create a font from that family. Once I had the TTF, I added it to my Visual Studio solution and set it to Content type/Copy to output always. ![]() I used an online converter to convert it to TTF. The first thing I did was use this method to pull down the WOFF file for the barcode font. Long ago, I blogged about hosting Google fonts locally. The particular project that I was working on is a website. ![]() Using the external Google font is not entirely straight-forward. One method in particular allows you to render a string to a bitmap. #How to make barcodes for images full#The System.Drawing namespace in C# is chock full of useful image manipulation and generation methods. #How to make barcodes for images pdf#However, in this particular application, I wound up needing to render the barcodes as images since the HTML that I render is fed into a PDF creator that doesn’t support font-face CSS stylings. Google has a nice “Code 128” barcode font which makes rendering a barcode in a website pretty easy. In one of my current projects, I needed to render some barcodes. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |