如何在 .NET 中访问打印机特定字体?

发布于 2024-07-18 02:30:53 字数 433 浏览 2 评论 0原文

这个问题与从.NET打印API检索设备上下文...

我有一个 Datacard 295 浮雕机/磁条编码器。 为了写入磁条或浮雕轮,您必须以特殊的“伪字体”写入文本,打印机驱动程序将识别并适当处理该文本。 有多种字体,具体取决于您要写入轨道 1、轨道 2、大浮雕字母还是小字母。

不幸的是,.NET 仅直接支持 OpenType 和 TrueType 字体。

与我提到的问题不同,我没有技术指南来告诉我要传输什么。 对我来说,处理这个问题最简单的方法是找到一种使用 .NET 中的打印机字体的方法,无论采取什么措施。 如何在 .NET 中访问和使用打印机字体?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-07-25 02:30:53

您无法直接从 .NET 执行此操作,因此必须在设备上下文上使用 Win32 调用来使用“伪字体”进行渲染。 示例代码位于此处 展示了如何执行此操作:

' 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文