如何在c#中使用PrintForm提高打印质量

时间:2013-06-01 07:10:56

标签: c# winforms vb6

我想打印窗体,然后我使用了两种方法,

1.使用visual basic power pack工具调用"PrintForm"

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

2.使用BitBlt函数

使用gdi32.dll

但是这两种方法的打印质量都很低,就像下面的图片一样。

enter image description here

但问题是我会这样做VB6并且它会以正确的打印方式正确打印

Private Sub Command1_Click()
  Me.PrintForm
End Sub

enter image description here

如何提高打印质量? (我正在使用带有Windows 7终极版的visual studio 2008 SP1)

1 个答案:

答案 0 :(得分: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);