0

I have a panel in which I have many controls.

I want to print this panel in window application.
How can i do this.
please send me code for it.

2
Contributors
1
Reply
8
Views
6 Years
Discussion Span
Last Post by nick.crane
1

Use Panel.DrawToBitmap to copy the panel to a bitmap and then print that.
You will need to re-order the controls in the panel as the DrawToBitmap method paints the controls in the wrong order. (Don't know why).
I use this code I got from another forum; I think it is only fair that I share it.;)

public static void InvertZOrderOfControls(Control.ControlCollection ControlList)
        {
            // do not process empty control list
            if (ControlList.Count == 0)
                return;

            // only re-order if list is writable
            if (!ControlList.IsReadOnly)
            {
                SortedList<int, Control> sortedChildControls = new SortedList<int, Control>();

                // find all none docked controls and sort in to list
                foreach (Control ctrlChild in ControlList)
                {
                    if (ctrlChild.Dock == DockStyle.None)
                        sortedChildControls.Add(ControlList.GetChildIndex(ctrlChild), ctrlChild);
                }

                // re-order the controls in the parent by swapping z-order of first 
                // and last controls in the list and moving towards the center
                for (int i = 0; i < sortedChildControls.Count / 2; i++)
                {
                    Control ctrlChild1 = sortedChildControls.Values[i];
                    Control ctrlChild2 = sortedChildControls.Values[sortedChildControls.Count - 1 - i];
                    int zOrder1 = ControlList.GetChildIndex(ctrlChild1);
                    int zOrder2 = ControlList.GetChildIndex(ctrlChild2);
                    ControlList.SetChildIndex(ctrlChild1, zOrder2);
                    ControlList.SetChildIndex(ctrlChild2, zOrder1);
                }
            }

            // try to invert the z-order of child controls
            foreach (Control ctrlChild in ControlList)
            {
                try { InvertZOrderOfControls(ctrlChild.Controls); }
                catch { }
            }
        }

Here is how to use it to get the bitmap.

Bitmap image = new Bitmap(panel1.Width, panel1.Height);

            InvertZOrderOfControls(panel1.Controls);

            panel1.DrawToBitmap(image, new Rectangle(new Point(), panel1.Size));

            InvertZOrderOfControls(panel1.Controls);

That is the easy bit.:P

To do the printing you need to use PrintDocument.
Do some research and have a go. Post again if you need more help.:)

Edited by nick.crane: corrected last invert to use panel1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.