views:

132

answers:

1

This question is along the same lines as Retrieving Device Context from .NET print API...

I have a Datacard 295 embosser / mag stripe encoder. In order to write to the Mag Stripe or Embosser wheel, you must write your text in a special "pseudo-font", which the printer driver will recognize and handle appropriately. There are multiple fonts, depending on whether you want to write to track 1, track 2, big embosser letters or small.

Unfortunately, .NET only directly supports OpenType and TrueType fonts.

Unlike the question I referenced, I have no tech guide to tell me what to transmit. The easiest way for me to handle the issue is to find a way to use the printer fonts from .NET, whatever that takes. How can I access and use printer fonts in .NET?

A: 

You can't do this directly from .NET, so you have to use Win32 calls on the device context to render using the "pseudo-font". The sample code available here shows how to do this:

' As we're using a device font, we need to write directly on the device context
' as the System.Drawing.Font class which is used to write on a graphics object
' does not support device fonts
Dim hdcLabel As IntPtr
hdcLabel = e.Graphics.GetHdc

' Create the new device font
Dim hfEPC As IntPtr
hfEPC = WinAPI.GDI32.CreateFont(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Track1")

' Select the font on the device context, getting a handle on the font that is being replaced
Dim hReplacedFont As IntPtr
hReplacedFont = WinAPI.GDI32.SelectObject(hdcLabel, hfEPC)

' Draw the text using the printer font
Dim intDrawTextReturn As Integer
intDrawTextReturn = WinAPI.User32.DrawText(hdcLabel, "Track 1 Data", ("Track 1 Data").Length, New Rectangle(20, 20, 300, 300), 0)

' Re-Select the original font on the device context
WinAPI.GDI32.SelectObject(hdcLabel, hReplacedFont)

' Dispose of the EPC font
WinAPI.GDI32.DeleteObject(hfEPC)

' Release the device context
e.Graphics.ReleaseHdc(hdcLabel)
Craig Lebakken