Skip to content

Instantly share code, notes, and snippets.

@tom-ficke
Created June 11, 2021 21:04
Show Gist options
  • Save tom-ficke/2c7ea33566f3a69db72ffba993456b4c to your computer and use it in GitHub Desktop.
Save tom-ficke/2c7ea33566f3a69db72ffba993456b4c to your computer and use it in GitHub Desktop.
Barcode Gen Test - PrivateFontCollection
<%@ Page Language="c#" %>
<%@ Import namespace="System"%>
<%@ Import namespace="System.Collections"%>
<%@ Import namespace="System.ComponentModel"%>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="System.Drawing"%>
<%@ Import namespace="System.Drawing.Text"%>
<%@ Import namespace="System.Drawing.Imaging"%>
<%@ Import namespace="System.Web"%>
<%@ Import namespace="System.Web.SessionState"%>
<%@ Import namespace="System.Web.UI"%>
<%@ Import namespace="System.Web.UI.WebControls"%>
<%@ Import namespace="System.Web.UI.HtmlControls"%>
<%
string sWorkPath = "";
sWorkPath = this.Context.Server.MapPath("");
// Get the Requested code to be created.
string Code = Request["code"].ToString();
// Multiply the lenght of the code by 40 (just to have enough width)
int w = Code.Length * 40;
// Create a bitmap object of the width that we calculated and height of 100
Bitmap oBitmap = new Bitmap(w,100);
// then create a Graphic object for the bitmap we just created.
Graphics oGraphics = Graphics.FromImage(oBitmap);
// Now create a Font object for the Barcode Font
// (in this case the IDAutomationHC39M) of 18 point size
PrivateFontCollection fnts = new PrivateFontCollection();
fnts.AddFontFile(sWorkPath + @"\IDAutomationHC39M.ttf");
FontFamily fntfam = new FontFamily("IDAutomationHC39M", fnts);
Font oFont = new Font(fntfam, 18);
// Font oFont = new Font("IDAHC39M Code 39 Barcode", 18);
// Let's create the Point and Brushes for the barcode
PointF oPoint = new PointF(2f, 2f);
SolidBrush oBrushWrite = new SolidBrush(Color.Black);
SolidBrush oBrush = new SolidBrush(Color.White);
// Now lets create the actual barcode image
// with a rectangle filled with white color
oGraphics.FillRectangle(oBrush, 0, 0, w, 100);
// We have to put prefix and sufix of an asterisk (*),
// in order to be a valid barcode
oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);
// Then we send the Graphics with the actual barcode
Response.ContentType = "image/jpeg" ;
oBitmap.Save (Response.OutputStream, ImageFormat.Jpeg);
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment