Skip to main content
 首页 » 编程设计

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

2024年02月05日8leader

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

请您参考如下方法:

您可以使用 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 
}