Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在打印预览中绘制边框,但边框不应打印C#_C#_.net_Image_Winforms_Printing - Fatal编程技术网

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

在打印预览中绘制边框,但边框不应打印C#,c#,.net,image,winforms,printing,C#,.net,Image,Winforms,Printing,我正在使用WinForms。在我的表格中,我有一个picturebox和一个按钮,用于打印picturebox中的图片 在“我的代码”中,当您单击“打印”按钮时,程序将显示一个打印预览,图像周围有一个矩形框。我画了这个长方形的盒子,因为我有特定种类的纸张可以打印。这些报纸的边框上有图片。用户无法在纸上打印图片。我只是想告诉用户,如果你在打印预览中通过这些矩形边界线,你将打印在纸上图片的顶部 目标:当我点击打印按钮时,我想看到一张带有矩形边框的打印预览纸,但我不想打印矩形边框。我只想把图像打印出来

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

在“我的代码”中,当您单击“打印”按钮时,程序将显示一个打印预览,图像周围有一个矩形框。我画了这个长方形的盒子,因为我有特定种类的纸张可以打印。这些报纸的边框上有图片。用户无法在纸上打印图片。我只是想告诉用户,如果你在打印预览中通过这些矩形边界线,你将打印在纸上图片的顶部

目标:当我点击打印按钮时,我想看到一张带有矩形边框的打印预览纸,但我不想打印矩形边框。我只想把图像打印出来

    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
    }

您可以使用该属性:

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
}
您可以使用该属性进行以下操作:

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
}


创建一个名为“drawBorder”的布尔变量,当您单击预览按钮时,该变量为true。我将尝试一下@LarsTech@taji01当点击打印按钮时,一定要将其重置为false;在预览按钮中单击{drawBorder=true}@ronbeyer创建一个名为“drawBorder”的布尔变量,当您单击预览按钮时,该变量为true。我将尝试一下@LarsTech@taji01当点击打印按钮时,一定要将其重置为false;在预览按钮中,点击{drawBorder=true}@RonBeyerIt仍然用这些边框线打印@LarsTech@taji01如果PictureBox有一个已绘制的边框,那么就必须将其删除。我在我的问题中添加了一张图片,以便让您看到视觉效果,因为我不认为我在其中添加了某种边框it@taji01不因为我看不到边界,所以视觉效果没有帮助。但很明显,从代码中,只有当drawBorder为true时,才会绘制边界。尝试对DrawRectangle进行注释,看看是否有边框。如果是的话,它是从别的地方来的。@taji01啊,现在我明白了。尝试
if(this.printDocument1.PrintController.IsPreview){…
取而代之。我没有测试这个。它仍然用那些边界线打印@LarsTech@taji01如果PictureBox有一个已绘制的边框,那么就必须将其删除。我在我的问题中添加了一张图片,以便让您看到视觉效果,因为我不认为我在其中添加了某种边框it@taji01不,我看不到博尔德,所以视觉效果不好r、 但是从代码中可以清楚地看出,只有当drawBorder为true时才会绘制边框。请尝试注释DrawRectangle,看看是否有边框。如果有,它来自其他地方。@taji01啊,现在我明白了。请尝试
if(this.printDocument1.PrintController.IsPreview){…
。我没有测试这个。