DrawToBitmap doesn't work on panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    DrawToBitmap doesn't work on panel

    So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.
    Code:
    void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                System.Drawing.Size oldSize = printData.Size;
    
                printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);
    
                InvertZOrderOfControls(printData.Controls);
                printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
                InvertZOrderOfControls(printData.Controls);
                
                e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
                bitmap.Save(@"C:\Users\jdudley\Documents\File.bmp");
                printData.Size = oldSize;
            }
    The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Did you try taking out the zorder change and see if it helps?

    Comment

    • Fr33dan
      New Member
      • Oct 2008
      • 57

      #3
      Sorry, I forgot about this post when no one responded and went to another forum.

      It turns out a control can only be printed after it has been added window and drawn. In order to get this code to work as intended I had to show the item off screen before drawing to a bitmap. But in case the item was already being show on a dialog I had to include code to re-add it to it's original parent.

      Code:
      void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
      {
          System.Drawing.Size oldSize = printData.Size;
          System.Drawing.Color oldBackcolor = printData.BackColor;
          Control oldParent = printData.Parent;
          printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
          printData.BackColor = Color.White;
      
          if (oldParent != null)
              oldParent.Controls.Remove(printData);
      
          handleForm = new Form();
          handleForm.Controls.Add(printData);
          handleForm.StartPosition = FormStartPosition.Manual;
          handleForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
      
          handleForm.Show();
      
          Bitmap bitmap = new Bitmap(printData.Width, printData.Height);
          printData.DrawToBitmap(bitmap, new Rectangle(new Point(0, 0), printData.Size));
          
          e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
      
          handleForm.Controls.Remove(printData);
          if (oldParent != null)
              oldParent.Controls.Add(printData);
          handleForm.Close();
      
          printData.Size = oldSize;
          printData.BackColor = oldBackcolor;
      }

      Comment

      Working...