2015-11-06 92 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