这个问题与Retrieving Device Context from .NET print API ...
一致我有一台Datacard 295压纹/磁条编码器。要写入Mag Stripe或Embosser滚轮,您必须使用特殊的“伪字体”编写文本,打印机驱动程序将识别并正确处理。有多种字体,具体取决于您是要写入曲目1,曲目2,大压纹字母还是小字体。
不幸的是,.NET只直接支持OpenType和TrueType字体。
与我引用的问题不同,我没有技术指南告诉我要传输什么。我处理这个问题的最简单方法是找到一种方法来使用.NET中的打印机字体,无论需要什么。如何在.NET中访问和使用打印机字体?
答案 0 :(得分:0)
您无法直接从.NET执行此操作,因此您必须在设备上下文中使用Win32调用来使用“伪字体”进行渲染。可用的示例代码here显示了如何执行此操作:
' 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)