c# - 如何从 Windows 窗体打印明文?

标签 c# winforms printing printdocument blurry

我已经成功地打印了一个 windows 窗体,但是所有的文字都有些模糊。我得出的结论是,这是屏幕分辨率远低于打印机使用的分辨率的结果。我的方法是否存在根本性缺陷,或者是否有办法在打印前重新格式化文本以使其清晰明了?

void PrintImage(object o, PrintPageEventArgs e)
{
    int x = SystemInformation.WorkingArea.X;
    int y = SystemInformation.WorkingArea.Y;
    int width = panel1.Width;
    int height = panel1.Height;

    Rectangle bounds = new Rectangle(x, y, width, height);

    Bitmap img = new Bitmap(width, height);

    this.DrawToBitmap(img, bounds);
    Point p = new Point(100, 100);
    e.Graphics.DrawImage(img, p);
}

private void BtnPrint_Click(object sender, EventArgs e)
{
    btnPrint.Visible = false;
    btnCancel.Visible = false;
    if(txtNotes.Text == "Notes:" || txtNotes.Text == "")
    {
        txtNotes.Visible = false;
    }
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);
    pd.Print();
}

最佳答案

Is there a fundamental flaw in my approach [...] ?

是的。

  1. 您使用 panel1 的大小来计算图像的大小。稍后,您让 this 绘制图像,但 this 是表单,而不是面板。

  2. 是什么让您认为 SystemInformation.WorkingArea 与您要打印的窗口相关?

  3. 您应该多注意一下一次性元素。

[...] is there a way to reformat the text prior to printing so that it comes out crisp?

没有一种通用方法可以让您同时缩放所有其他控件。

但是,通过使用 NearestNeighbor 机制将位图放大一定比例,您可以获得清晰的像素化文本,而不是模糊的文本。

这是在 Acrobat Reader 中以相同缩放级别生成的未缩放(左)和 3 倍缩放(右)的 PDF 的区别(点击放大):

Image in result.

这是缩放代码,同样没有解决任何一次性问题:

        this.DrawToBitmap(img, bounds);
        Point p = new Point(100, 100);
        img = ResizeBitmap(img, 3);
        e.Graphics.DrawImage(img, p);
    }

    private static Bitmap ResizeBitmap(Bitmap source, int factor)
    {
        Bitmap result = new Bitmap(source.Width*factor, source.Height*factor);
        result.SetResolution(source.HorizontalResolution*factor, source.VerticalResolution*factor);
        using (Graphics g = Graphics.FromImage(result))
        {
            g.InterpolationMode = InterpolationMode.NearestNeighbor;
            g.DrawImage(source, 0, 0, source.Width*factor, source.Height*factor);
        }
        return result;
    }

关于c# - 如何从 Windows 窗体打印明文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55750496/

相关文章:

c# - 在两个集合中搜索

java - 在双面模式下使用 Java 打印横向文档

JavaScript 在 Internet Explorer 8(和 7)中为 window.print 调用和应用

c# - 如何在详细 View 中为命令字段添加删除确认提示?

c# - 如何根据提供的数据将数据显示到gridview中?

c# - 使用属性中的格式字符串

winforms - 我可以强制安装程序项目使用构建解决方案中的 .config 文件而不是原始解决方案吗?

c# - WinForm 尝试提交来自 CellValueChanged 事件的 DataGridView 更改

html - 在 chrome 中打印纯 HTML 时不同的字体大小

c# - System.Text.Encoding.GetEncoding ("iso-8859-1") 抛出 PlatformNotSupportedException?