在打印预览中绘制边框但边框不应打印C#

时间:2015-11-06 22:41:38

标签: c# .net image winforms printing

我正在使用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
    }

enter image description here

1 个答案:

答案 0 :(得分:4)

您可以使用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
}
相关问题