c# - WPF 使用 Magstrip 编码打印到打印机

标签 c# wpf printing image-processing

我正在编写一个新的 WPF 应用程序来创建一些视觉元素,然后尝试在 Poloroid 打印机上打印它们。我使用 WPF 中的 System.Printing 类成功打印如下:

Dim Pd As PrintDialog = New PrintDialog()
Dim Ps = New LocalPrintServer()
Dim PrintQueue = New PrintQueue(Ps, "Poloroid Printer")
Pd.PrintQueue = PrintQueue
Pd.PrintVisual(Me.Grid1, "Print Job 1")  'this prints out perfectly

问题是 poloroid 打印机有一个 SDK,您可以使用它来写入卡片背面的 Magstrip。我有一个在 System.Drawing.Printing 中使用 PrintPageEventArgs 的工作示例,但我找不到与 WPF 世界任何接近的匹配项。这是代码:

Private Sub PrintPage(ByVal sender as Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  '...

  ' Obtain the device context for this printer
  deviceContext = e.Graphics.GetHdc().ToInt32()

  '... device context is used in SDK to write to magstrip

  e.Graphics.ReleaseHdc(New IntPtr(deviceContext))
End Sub

所以,我的问题是:

如何使用 System.Drawing.Printing 打印我现有的标记 (XAML)

System.Printing 中是否有与 SDK 对话并获取 Int32 deviceContext 的事件?

我尝试使用 RenderTargetBitmap 类将 WPF 中的视觉呈现为位图并将位图转换为 System.Drawing.Bitmap

RenderTargetBitmap bitmapSource;
...
bitmapSource.Render(visual);
...
using(MemoryStream outStream = new MemoryStream())
{
 BitmapEncoder enc = new BmpBitmapEncoder();
 enc.Frames.Add(BitmapFrame.Create(bitmapSource));
 enc.Save(outStream);
 System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
 ...
}

但是打印的不是很清晰和完美。

最佳答案

试试这个

      Rect bounds = VisualTreeHelper.GetDescendantBounds(FrontCanvas);
      double dpiX =e.Graphics.DpiX  , dpiY=e.Graphics.DpiY ;
      RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX  / 96.0),
                                                      (int)(bounds.Height * dpiY   / 96.0),
                                                      dpiX,
                                                      dpiY,
                                                      PixelFormats.Pbgra32);

      DrawingVisual dv = new DrawingVisual();
      using (DrawingContext ctx = dv.RenderOpen())
      {
          VisualBrush vb = new VisualBrush(FrontCanvas);
          ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
      }

      rtb.Render(dv);

      //System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rtb);
      //e.Graphics.DrawImage(rtb, 0, 0);

      using (MemoryStream outStream = new MemoryStream())
      {
          // Use png encoder for  data
          BitmapEncoder encoder = new PngBitmapEncoder();

          // push the rendered bitmap to it
          encoder.Frames.Add(BitmapFrame.Create(rtb));
          encoder.Save(outStream);
          System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
          e.Graphics.DrawImage(bitmap, 0, 0);

      }

关于c# - WPF 使用 Magstrip 编码打印到打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4657431/

相关文章:

c# - C# 中的 IHostedService 不会每次都触发方法吗?

c# - 如何将多个表数据结构合并到一个表结果结构中?

c# - 获取 WPF 路径的长度

c# - 绑定(bind)组合框 MVVM

javascript - 从网站上使用 DYMO 标签打印机打印

c# - 从服务器加载时使 Texture2D 在 Unity 中可读

c# - 带空值的 EF Core 复合主键

c# - 在一段时间内禁用所有执行

c# - 将文件发送到打印机,没有打印任何内容

python - 通过用户输入设置打印的小数位数