Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Arrow Printing Panel controls not showing correctly

    I have so far tried:
    Code:
    public void DrawControl(Control control,Bitmap bitmap)
        {
            control.DrawToBitmap(bitmap,control.Bounds);
            foreach (Control childControl in control.Controls)
            {
                DrawControl(childControl,bitmap);
            }
        }
    
        public void SaveBitmap()
        {
            Bitmap bmp = new Bitmap(this.panel1.Width, this.panel.Height);
    
            this.panel.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel.Width, this.panel.Height));
            foreach (Control control in panel1.Controls)
            {
                DrawControl(control, bmp);
            }
    
            bmp.Save("d:\\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    and in the printClick I used SaveBitmap(); however when I look at the print preview or the image that I saved to test if it was working, my panel4 controls are printed in reverse order than what I have coded in my designer PictureBox1 is shown behind all of the other controls that have added, because it is like a background image(since its a paint type application) I have labels and other pictureBox controls which show up perfectly on the form design but when I go to print them its as though it draws each control one by one, and the last one being as its huge gets drawn last and basically erases all of the controls that I had drawn.

    Is there a way to fix this, or should I try printing my panel4 controls differently? Thx

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,837
    Thanks
    42
    Thanked 199 Times in 198 Posts
    this should more than get you close- I'll leave it to you to work on a bit and see if you can go the extra step
    Code:
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
            }
    
            public void focusPB1()
            {
                pictureBox1.Focus();
            }
    
            public void pictureBox_Click(object sender, System.EventArgs e)
            {
                PictureBox CurPB = ((PictureBox)sender);
                CurPB.Focus();
                if (CurPB.BackgroundImage == null)
                {
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        Image myBG = Image.FromFile(openFileDialog1.FileName);
                        CurPB.BackgroundImage = myBG;
                    }
                }
                else
                {
                    printDocument1.Print();
                }
            }
    
            void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                throw new System.NotImplementedException();
            }
        }
    desisgner.cs
    Code:
            private void InitializeComponent()
            {
                this.pictureBox1 = new System.Windows.Forms.PictureBox();
                this.pictureBox2 = new System.Windows.Forms.PictureBox();
                this.pictureBox3 = new System.Windows.Forms.PictureBox();
                this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                this.printDocument1 = new System.Drawing.Printing.PrintDocument();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
                this.SuspendLayout();
                // 
                // pictureBox1
                // 
                this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.pictureBox1.Location = new System.Drawing.Point(35, 31);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(134, 128);
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
                this.pictureBox1.Click += new System.EventHandler(this.pictureBox_Click);
                // 
                // pictureBox2
                // 
                this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.pictureBox2.Location = new System.Drawing.Point(219, 31);
                this.pictureBox2.Name = "pictureBox2";
                this.pictureBox2.Size = new System.Drawing.Size(147, 128);
                this.pictureBox2.TabIndex = 1;
                this.pictureBox2.TabStop = false;
                this.pictureBox2.Click += new System.EventHandler(this.pictureBox_Click);
                // 
                // pictureBox3
                // 
                this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.pictureBox3.Location = new System.Drawing.Point(419, 31);
                this.pictureBox3.Name = "pictureBox3";
                this.pictureBox3.Size = new System.Drawing.Size(145, 128);
                this.pictureBox3.TabIndex = 2;
                this.pictureBox3.TabStop = false;
                this.pictureBox3.Click += new System.EventHandler(this.pictureBox_Click);
                // 
                // openFileDialog1
                // 
                this.openFileDialog1.FileName = "openFileDialog1";
                // 
                // printDocument1
                // 
                this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(606, 187);
                this.Controls.Add(this.pictureBox3);
                this.Controls.Add(this.pictureBox2);
                this.Controls.Add(this.pictureBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.PictureBox pictureBox1;
            private System.Windows.Forms.PictureBox pictureBox2;
            private System.Windows.Forms.PictureBox pictureBox3;
            private System.Windows.Forms.OpenFileDialog openFileDialog1;
            private System.Drawing.Printing.PrintDocument printDocument1;

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    crank01 (02-14-2012)

  • #3
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    thanks again alykins and I'm guessing .Focus could work just as well with panel as it can with pictureBox. I never used focus before but I should be able to understand what is happening in the code you have written for me.

    I'm not sure but it looks like printDocument1 would print the entire form, unless .focus plays a part in passing the pictureBoxes image.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,837
    Thanks
    42
    Thanked 199 Times in 198 Posts
    what I have there is ground work to setting up the print method- right now all it does is que a page to be printed (nothing really prints- on my network printer it signals a que from my machine and in turn sends a cover letter print job) the focus() method sets focus you need to figure out how to print that focused object

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by alykins View Post
    what I have there is ground work to setting up the print method- right now all it does is que a page to be printed (nothing really prints- on my network printer it signals a que from my machine and in turn sends a cover letter print job) the focus() method sets focus you need to figure out how to print that focused object
    oh so using focus() I could print a certain part(panel4) of the form. Now do I even need to use draw to bitmap, or is that not needed since I have the print method and I need to aim the focus of print() at the panel4.

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,837
    Thanks
    42
    Thanked 199 Times in 198 Posts
    using focus you can "focus" an object and get information about it (ie it's position on the screen) then in the printdocument class there are parameters for setting margins (ie the coordinates of where you want to print)

    I guess as a quick and dirty you could set everything to hidden (except what you want to print) and then use PrintForm

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #7
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by alykins View Post
    using focus you can "focus" an object and get information about it (ie it's position on the screen) then in the printdocument class there are parameters for setting margins (ie the coordinates of where you want to print)

    I guess as a quick and dirty you could set everything to hidden (except what you want to print) and then use PrintForm
    Lol that would be pretty quick and dirty but no way I am gunna try it that way. I'll try you're way and let u know if I have any problems, which *crossing fingers* I wont

  • #8
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Woot! I figured it out =) I'll post how I succeeded in finally printing the panel tomorrow
    Last edited by crank01; 02-21-2012 at 04:53 AM.

  • #9
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts

    How to Print Panel+panel Controls

    I hope I'm helping someone here bcuz this was bothering me for sometime now

    Code:
    private void print1_Click(object sender, EventArgs e)
            {
    PrintDocument printPanel = new PrintDocument();
                printPanel.PrintPage += new PrintPageEventHandler(paneldraw);
                
    
    
                PrintPreviewDialog printPanelx = new PrintPreviewDialog();
                printPanelx.Document = printPanel;
                printPanelx.ShowDialog();
            }
    private void paneldraw(object sender, PrintPageEventArgs e)
            {
                Bitmap bitmapP = new Bitmap(panel1.Width, panel1.Height);
    Rectangle rectangle = new Rectangle(0, 0, bitmapP.Width, bitmapP.Height);
                
                InvertZOrderOfControls(panel1.Controls);
                panel1.DrawToBitmap(bitmapP, new Rectangle(new Point(), panel1.Size));
                Point p = new Point(100, 100);
                InvertZOrderOfControls(panel1.Controls);
                e.Graphics.DrawImage(bitmapP, rectangle);
    }
    public 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 { }
                }
            }
    What InvertZOrderOfControls does is it inverts the order of the Controls that you added in the designer briefly before you draw to the bitmap, because if you didn't reverse the order(after countless hours of understanding it ) it draws the controls to the bitmap in the wrong order. Which means if your Panel has controls, one ontop of the other(layered) it will draw the control from Top to Bottom.

    So if you have a pictureBox acting like the background image of your Panel, and label on top of it. It will draw the label 1st, and then the large background image onto the bitmap, which means you won't be able to even see the label when it is printed.

    That is where the InvertZOrderOfControls method comes into play, which reverts the order momentarily and all is well again

    If this helped you in any way, please Thank me for the post. I'm shooting for atleast one lol


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •