如何将PictureBox.Image放入PrintPreviewDialog?

时间:2015-04-16 06:44:03

标签: c# picturebox print-preview

我一直在这里寻找答案,但我发现的是如何打印图像,而不是如何将图像转换为printPreviewDialog。它应该很简单:

    private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = pictureBoxMain.Image; 
        printPreviewDialog1.ShowDialog();      
    }

然而,我收到错误

  

"Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Printing.PrintDocument'"

没有内置"Convert"来处理此转换。那么,我如何从pictureBox进行简单的打印预览?

编辑:好的,打印预览现在正在运行。打印无效。我担心的一个问题是我没有" OK"在我的打印对话框中,我有"应用","取消"和"打印"。 Visual Studio不接受DialogResult.Print作为可接受的响应(编译器错误)。这是我目前的代码,欢迎推荐,我显然不太明白。感谢

//Working now    
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
    {
        printDocument1.DocumentName = pictureBoxMain.Image.ToString();
        printDialog1.Document = printDocument1;
        printPreviewDialog1.Document = printDialog1.Document;   
        printPreviewDialog1.ShowDialog();      
    }

//Not printing 
private void printToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        printDocument1.OriginAtMargins = true;
        printDocument1.DocumentName = pictureBoxMain.Image.ToString();
        if (printDialog1.ShowDialog() == DialogResult.OK)  
            printDialog1.Document = printDocument1;        
    }

//If this is needed, I cannot find a way to call this from within the print menu click event without generating an error, EventArgs e does not get along with PrintPageEventArgs e.  
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(pictureBoxMain.Image, 0, 0);
    }
编辑:我尝试进行以下更改以测试问题是否缺少“确定”按钮。它仍然没有打印。所以我不认为缺少OK按钮就是问题所在。

 if (printDialog1.ShowDialog() != DialogResult.Cancel)  
     printDialog1.Document = printDocument1;   

谢谢

0 个答案:

没有答案