繁体   English   中英

WPF使用Magstrip编码打印到打印机

[英]WPF Printing to printer with Magstrip encoding

我正在编写一个新的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);
 ...
}

但是印刷并不清晰和完美。

我最近回答了一个类似的问题,可以在这里找到: https : //stackoverflow.com/a/43373398/3393832 该问题和答案与磁编码(在ID卡上)的关系更多,而不是与图像打印有关,但我还将在此处尝试提供一些见解。

对于卡片图像,我实际上在XAML中设置了两个网格:一个外部网格,其中容纳了所有内容(按钮,标签,组合框,内部网格),而一个内部网格则完全是我要应用的图像,在卡本身上。 在代码中,添加/更新了所有元素后,我对内部网格进行了“快照”,并将该图像发送到“打印作业”中。

为了继续上一篇文章(上面的链接),我在卡的第一面(PageCount == 0)的磁性编码步骤之后添加了图像打印。

以下是一些我用来获取Grid / Full Image的位图“快照”而不会造成清晰度/分辨率或像素化损失的代码:

RenderTargetBitmap rtb = new RenderTargetBitmap(gridWidth, gridHeight, 210, 210, PixelFormats.Pbgra32);

rtb.Render(YourGrid);

// If you need to Crop Anything Out
CroppedBitmap crop = new CroppedBitmap();

crop = new CroppedBitmap(rtb, new Int32Rect(0, 0, gridWidth, gridHeight));

Bitmap bmpToPrint = BitmapSourceToBitmap(crop);

// Helper Method
public Bitmap BitmapSourceToBitmap(BitmapSource bs)
{
    Bitmap bitmap;

    using (MemoryStream ms = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bs));
        enc.Save(ms);
        bitmap = new Bitmap(ms);
    }

    return bitmap;
}

对于电磁编码,我使用e.Graphics.DrawString,对于图像(您猜对了),使用e.Graphics.DrawImage。 只需在DrawImage之前调用DrawString,您的打印机驱动程序就应该在PrintPage方法中获取图像之前的编码(同样,请参阅上面的链接以获取更多我正在使用的Print方法的详细信息。该方法也不受限制)打印机品牌或型号,并且不依赖任何SDK)。

尽管是通用的并且需要您特定的实现细节,但我希望本文能指导您找到适合您特定需求的解决方案。 请随时询问您是否有任何后续问题,或者需要更多详细信息来了解打印作业本身的特定元素。

尝试这个

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

      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM