2013-06-01 31 views
0

我想打印Windows窗體,然後我用了兩種方法,如何提高打印質量與PrintForm在c#

1.使用Visual Basic中的電源組刀具調用"PrintForm"

private void btnPriint_Click(object sender, EventArgs e) 
     { 
      printForm1.Print(); 
     } 

2.Used gdi32.dllBitBlt功能

但是這兩種方法得到的打印質量低下等爲波紋管的畫面。

enter image description here

但事情是我會做VB6,它會用清晰的打印正確打印

Private Sub Command1_Click() 
    Me.PrintForm 
End Sub 

enter image description here

如何提高打印的品質呢? (我用與最終的Windows 7的Visual Studio 2008 SP1)

+3

看起來像你打印您的表單元素的圖像。考慮到你的平均桌面屏幕至多100dpi,而你可能瞄準的打印機至少300dpi,你總是會得到糟糕的質量。這意味着不使用屏幕渲染打印源。 –

+0

我該怎麼辦? – Elshan

+0

@Elshan它是winforms嗎? – Leri

回答

1

您可以創建位圖圖像渲染像素的一種形式:

// Assuming this code is within the form code-behind, 
// so this is instance of Form class. 
using (var bmp = new System.Drawing.Bitmap(this.Width, this.Height)) 
{ 
    this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height)); 
    bmp.Save("formScreenshot.bmp"); //or change another format. 
} 

爲了保持乾淨,你可以創建擴展方法。例如:

public static class FormExtentions 
{ 
    public static System.Drawing.Bitmap TakeScreenshot(this Form form) 
    { 
     if (form == null) 
      throw new ArgumentNullException("form"); 

     form.DrawToBitmap(bmp, new Rectangle(0, 0, form.Width, form.Height)); 

     return bmp; 
    } 

    public static void SaveScreenshot(this Form form, string filename, System.Drawing.Imaging.ImageFormat format) 
    { 
     if (form == null) 
      throw new ArgumentNullException("form"); 
     if (filename == null) 
      throw new ArgumentNullException("filename"); 
     if (format == null) 
      throw new ArgumentNullException("format"); 

     using (var bmp = form.TakeScreenshot()) 
     { 
      bmp.Save(filename, format); 
     } 
    } 
} 

形式的代碼隱藏內部用法:

this.SaveScreenshot("formScreenshot.png", 
        System.Drawing.Imaging.ImageFormat.Png); //or other formats 

注:DrawToBitmap將只繪製屏幕上顯示內容。

編輯:而在OP影像png你可以使用:bmp.Save("formScreenshot.png", System.Drawing.Imaging.ImageFormat.Png);

+0

是的,它的工作fine.but事情是更盤帶作品 – Elshan

+0

@Elshan你想減少代碼?如果是這樣,你可以創建擴展方法。 – Leri

+0

如何獲得該圖像的打印預覽? – Elshan