2015-11-06 67 views
1

我正在使用WinForms。在我的表格中,我有一个图片框和一个可以在图片框中打印图片的按钮。在打印预览中绘制边框但边框不应打印C#

在我的代码中,当您单击打印按钮时,程序会在图像周围显示一个矩形框的打印预览。我画了这个矩形框,因为我有特定类型的纸张,我打印到。这些报纸在边界上有图片。用户无法打印纸张上的图片。我只想通知用户,如果您在打印预览中传递这些矩形边界线,您将在纸张上的图片上打印。

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

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

创建一个名为“drawBorder”布尔变量是真的当你点击预览按钮。 – LarsTech

+0

我要试试@LarsTech – taji01

+0

@ taji01点击打印按钮时,一定要将它重置为false。 –

回答

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 
} 
+0

它仍然打印与这些边界线@LarsTech – taji01

+0

@ taji01如果PictureBox有一个画边框,那么将不得不被删除。 – LarsTech

+0

我在我的问题上添加了一张图片,让您看到视觉效果,因为我不认为我在其中添加了某种边框 – taji01