【问题标题】:Draw Borders in Print Preview but the Borders Should not Print C#在打印预览中绘制边框但边框不应打印 C#
【发布时间】:2015-11-06 22:41:38
【问题描述】:

我正在使用 WinForms。在我的表单中,我有一个图片框和一个打印图片框中图片的按钮。

在我的代码中,当您单击打印按钮时,程序会在图像周围显示一个带有矩形框的打印预览。我画了这个矩形框,因为我有要打印的特定类型的纸张。这些文件的边框上有图片。用户不能在纸上打印图片。我只是想告诉用户,如果您在打印预览中传递这些矩形边界线,您将在纸上的图片之上打印。

目标:当我单击打印按钮时,我希望看到带有矩形边框的打印预览纸,但我不想打印矩形边框。我只想打印图像。

    private void button1_Click(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        var bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
        this.pictureBox1.DrawToBitmap(bmp, this.pictureBox1.ClientRectangle);
        e.Graphics.DrawRectangle(Pens.Salmon, 25, 25, 500, 1000);
        e.Graphics.DrawImage(bmp, 25, 25, 500, 500); //Gets the input from the textboxes
    }

【问题讨论】:

  • 创建一个名为“drawBorder”的布尔变量,当您单击预览按钮时该变量为真。
  • 我要试试@LarsTech
  • @taji01 请务必在单击打印按钮时将其重置为 false。
  • 我有点迷路了,但是像这样,bool drawBorder = false;并在 preview_button_click{ drawBorder = true} @RonBeyer

标签: c# .net image winforms printing


【解决方案1】:

您可以为此使用PrintController.IsPreview 属性:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    var bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
    this.pictureBox1.DrawToBitmap(bmp, this.pictureBox1.ClientRectangle);
    if (this.printDocument1.PrintController.IsPreview) {
      e.Graphics.DrawRectangle(Pens.Salmon, 25, 25, 500, 1000);
    }
    e.Graphics.DrawImage(bmp, 25, 25, 500, 500); //Gets the input from the textboxes
}

【讨论】:

  • 它仍然用那些边框线打印@LarsTech
  • @taji01 如果 PictureBox 有彩绘边框,则必须将其移除。
  • 我在我的问题上添加了一张图片让您看到视觉效果,因为我不认为我在其中添加了某种边框
  • @taji01 不,视觉没有帮助,因为我没有看到边框。但从代码中可以清楚地看出,只有在 drawBorder 为 true 时才会绘制边框。尝试注释掉 DrawRectangle 并查看是否有边框。如果你这样做了,它来自其他地方。
  • @taji01 啊,现在我明白了。请改用if (this.printDocument1.PrintController.IsPreview) {...。我没有对此进行测试。
猜你喜欢
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 2021-07-10
  • 1970-01-01
相关资源
最近更新 更多