2011-01-11 90 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)。

雖然通用且需要您的特定實現細節,但我希望這篇文章能夠幫助您指導符合您特定需求的解決方案。請毫不猶豫地詢問您是否有任何後續問題或需要有關打印作業本身特定元素的更多細節。