Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Con risposta Print Dialog problem?

  • martedì 1 maggio 2012 14:57
     
     

    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)
                {

                }


            }

    • Spostato CoolDadTxMVP mercoledì 2 maggio 2012 14:45 Winforms related (From:Visual C# General)
    •  

Tutte le risposte

  • martedì 1 maggio 2012 16:02
     
     

    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".

  • martedì 1 maggio 2012 17:44
     
     
    When 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?
  • mercoledì 2 maggio 2012 14:32
     
     Con risposta Contiene codice

    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!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us


  • venerdì 4 maggio 2012 17:57
     
     Con risposta
    Thank you, this solution worked well.
    • Contrassegnato come risposta Ed Jurz venerdì 4 maggio 2012 17:58
    •  
  • sabato 5 maggio 2012 07:16
     
     

    You're welcome!  :)

    Have a nice weekend!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us