Print Dialog problem?
-
Tuesday, May 01, 2012 2:57 PM
I have been trying use the print dialog on my winforms app to suppress color and it doesn't not work. I have included print snippet code below. Could someone please steer me in the right direction.
private void buttonPrintPage_Click(object sender, EventArgs e)
{
PrintDocument printDocument1 = new PrintDocument();
var pDialog = new PrintDialog();
printDocument1.DocumentName = "Document Name";
pDialog.Document = printDocument1;
pDialog.AllowSelection = true;
pDialog.AllowSomePages = true;
pDialog.PrinterSettings.DefaultPageSettings.Color = false;
pDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
HideProgressBars();
CaptureScreen();
if (pDialog.ShowDialog() == DialogResult.OK)
{
printDocument1.PrinterSettings = pDialog.PrinterSettings;
printDocument1.PrintPage += new PrintPageEventHandler
(this.printDocument1_PrintPage);
printDocument1.Print();
}
}
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
PrintDocument document = (PrintDocument)sender;
e.PageSettings.PrinterSettings = document.PrinterSettings;
e.Graphics.DrawImage(memoryImage, 0, 0);
}
catch (InvalidPrinterException)
{
}
}
- Moved by CoolDadTxMVP Wednesday, May 02, 2012 2:45 PM Winforms related (From:Visual C# General)
All Replies
-
Tuesday, May 01, 2012 4:02 PM
Hi,
Do you mean print not in color? msdn document says setting value PageSettings.Color = false will do this,
http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.color.aspx
Does this helps you?
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
-
Tuesday, May 01, 2012 5:44 PMWhen presented with the print dialog properties, I change the property from printing in color to print in Gray scale, the result of the printed output is still in color. Am I missing something?
-
Wednesday, May 02, 2012 2:32 PM
Hi,
It seems that PageSettings.Color = false does not work in this case. Here is a similar thread, http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/a47e3765-660f-4d31-b871-8af54bbc82cf/
Besides the workaround mentioned in the above thread, we can also consider converting the image into black & white
public Bitmap MakeGrayscale3(Bitmap original) { //create a blank bitmap the same size as original Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix ColorMatrix colorMatrix = new ColorMatrix( new float[][] { new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[] {.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); //create some image attributes ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image //using the grayscale color matrix g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object g.Dispose(); return newBitmap; } private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { try { memoryImage = MakeGrayscale3(memoryImage); PrintDocument document = (PrintDocument)sender; e.PageSettings.PrinterSettings = document.PrinterSettings; e.Graphics.DrawImage(memoryImage, 0, 0); } catch (InvalidPrinterException) { } }The method I used to convert the image refers to this blog, http://notes.ericwillis.com/2009/11/make-an-image-black-and-white-in-csharp/
Good day!
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
- Edited by Michael Sun [MSFT]Microsoft Employee Wednesday, May 02, 2012 2:56 PM
- Proposed As Answer by Michael Sun [MSFT]Microsoft Employee Friday, May 04, 2012 6:17 AM
- Marked As Answer by Ed Jurz Friday, May 04, 2012 5:56 PM
-
Friday, May 04, 2012 5:57 PM
Thank you, this solution worked well.- Marked As Answer by Ed Jurz Friday, May 04, 2012 5:58 PM
-
Saturday, May 05, 2012 7:16 AM


