views:

24

answers:

1

I'm trying to print an Image using PrintDocumentin C# but somehow the setting (like Number of Pages and Image Quality ) are ignored while printing and preview.

Is there anything wrong in following code, Am I missing something?

private void button1_Click(object sender, EventArgs e)
{
    using (var printDialog = new PrintDialog())
    {
        if (printDialog.ShowDialog() == DialogResult.OK)
        {
            _printDocument.PrinterSettings = printDialog.PrinterSettings;    
        }
    }
}

void _printDocument_Print(object sender, PrintPageEventArgs e)
{
    using (Image image = new Bitmap("image0002.tif"))
    {
        e.Graphics.DrawImage(image, e.MarginBounds.X, e.MarginBounds.Y);
    }
}
+1  A: 

Have you tried setting the PrintDialog's Document property to the document you want to print? The dialog should automatically manage the settings for the current PrintDocument if I remember correctly, so there should be no need to manually assign the PrinterSettings.

In addition, I think that a DialogResult.OK from PrintDialog.ShowDialog() means that you should print the document (the user clicked the 'Print' button).

For example:

using (var printDialog = new PrintDialog { Document = _printDocument }) 
{ 
    if (printDialog.ShowDialog() == DialogResult.OK) 
    { 
        _printDocument.Print();     
    } 
} 

Does that help?


EDIT: If you don't want to print right away, you could try:

using (var printDialog = new PrintDialog { Document = _printDocument }) 
{ 
    printDialog.ShowDialog();     
} 

but users might find it a little strange if they click 'Print' and the document doesn't print.

Alex Humphrey
That did help in understanding the way PrintDialog works, but is there any way I can set the `PrinterSettings` to document with `PrintDialog` and later print it with another button click or later?
Prashant
@Prashant - I guess you could just show the `PrintDialog` and ignore the result, letting it interact with the document's settings how it sees fit. I'm guessing that if the user clicks 'Cancel', the changes will be thrown away, anything else, and they should be applied, but it's been a while since I've worked with this stuff. See my edit.
Alex Humphrey