2011-01-11 88 views
2

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

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打印机,您可以使用写入Magstrip在卡背面的SDK。我有一个使用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

OR

我现有的标记(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); 
... 
} 

但印刷不清晰,完美。

+0

相关:http:// stackoverflow。COM /问题/ 4544035/ID卡,磁条编码 – naveen 2011-01-12 12:29:58

回答

0

试试这个

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

     } 
0

我最近回答了类似的问题,可以在这里找到:https://stackoverflow.com/a/43373398/3393832。这个问题和答案更多地涉及磁性编码(在身份证上),而不是图像打印,但我也会尝试在此提供一些见解。

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

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

下面是一些代码,我用它来获得网格/全图的位图“快照”不清晰/分辨率的任何损失或任何像素化:

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

对于磁性编码,我使用电子邮件.Graphics.DrawString和Image(你猜对了)e.Graphics.DrawImage。只需在DrawImage之前调用DrawString,您的打印机驱动程序应该在PrintPage方法内的图像之前选择编码(同样,请参阅上面的链接,了解我使用的Print方法的更多细节。此方法也不受打印机品牌或型号,并且不依赖于任何SDK)。

虽然通用且需要您的特定实现细节,但我希望这篇文章能够帮助您指导符合您特定需求的解决方案。请毫不犹豫地询问您是否有任何后续问题或需要有关打印作业本身特定元素的更多细节。