How to make lines appear in front of picturebox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrcw
    New Member
    • Nov 2008
    • 82

    How to make lines appear in front of picturebox?

    Code:
    namespace TestControl
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
              private void Form1_Load(object sender, EventArgs e)
            {
                this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            }
    
              protected override void OnPaint(PaintEventArgs e)
              {
                  drawRobot();
              }
    
            private void drawRobot()
            {
             Pen MaplinePen = new Pen(System.Drawing.Color.LightGray);
                Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
    
                int RobotOriginX = (this.ClientSize.Width / 2); // -(RobotellipseWidth / 2);
                int RobotOriginY = (this.ClientSize.Height / 2); // -(RobotellipseHeight / 2);
              
                int RobotellipseWidth = 20;
                int RobotellipseHeight = 20;
                
                Graphics grphx = this.CreateGraphics();
                             
                //Draw RobotElipse
                for (int n = 0; n < 5; n++)
                {
                    grphx.DrawEllipse(RobotEllipsePen,
                    RobotOriginX - (RobotellipseWidth / 2),
                    RobotOriginY - (RobotellipseHeight / 2),
                    (RobotellipseWidth + n),
                    (RobotellipseHeight + n));
                    RobotellipseWidth = RobotellipseWidth + 20;
                    RobotellipseHeight = RobotellipseHeight + 20;
                }
           }
        
            private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
                openFileDialog1.Title = "Open Image File";
                openFileDialog1.Filter = "Bitmap Files|*.bmp" +
                    "|Enhanced Windows MetaFile|*.emf" +
                    "|Exchangeable Image File|*.exif" +
                    "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" +
                    "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";
    
                openFileDialog1.FilterIndex = 6;
                openFileDialog1.FileName = "";
                openFileDialog1.ShowDialog();
    
                if (openFileDialog1.FileName == "")
                    return;
    
                // set the extended picturebox control's
                // image to the open file dialog's filename
                this.xtendPicBox1.PictureFile = openFileDialog1.FileName;
            }
    
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Dispose();
            }  
     
        }
    }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Instead of creating a new graphics object, use the one that comes with the parameter of the OnPaint method you over rode. It's in the PaintEventArgs parameter as a property named Graphics.

    Then you can update your drawRobot method to take a Graphics parameter.

    Code:
              protected override void OnPaint(PaintEventArgs e)
              {
                  drawRobot(e.Graphics);
              }
    Code:
            private void drawRobot(Graphics g)
            {
                Graphics grfx = g;
                ...
            }
    It's also worth noting here that you are drawing on the form, not the PictureBox. If you want to draw on the PictureBox, you will need to use the Paint event of that control.

    Comment

    • mrcw
      New Member
      • Nov 2008
      • 82

      #3
      Hi, I put those commands in, but the robot is still drawn behind the .jpg. I don't know why this is
      thanks

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        As I mentioned, you're currently drawing on the form. So it's quite likely that your PictureBox will draw over top. Instead of using the Form's paint event, use the one on the PictureBox itself. There should be an OnPaint event you can use.

        Comment

        • mrcw
          New Member
          • Nov 2008
          • 82

          #5
          I think I've tried that (look at the final two methods. ps I redesigned the form to make it easier for me to understand.

          Code:
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.Text;
          using System.Windows.Forms;
          
          namespace xpic3
          {
              public partial class Form1 : Form
              {
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  private void Form1_Load(object sender, EventArgs e)
                  {
                      this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
                      pictureBox1.Image = Image.FromFile("c:/Users/m/Desktop/map-blank.jpg");
                      hScrollBar.Minimum = 0;
                      hScrollBar.Maximum = pictureBox1.Width;
                      vScrollBar.Minimum = 0;
                      vScrollBar.Maximum = pictureBox1.Height;
                   }
          
                  private void vScrollBar1_ValueChanged(object sender, EventArgs e)
                  {
                      pictureBox1.Top = vScrollBar.Value;
                  }
          
                  private void hScrollBar_ValueChanged(object sender, EventArgs e)
                  {
                      pictureBox1.Left = hScrollBar.Value;
                  }
          
                  private void drawRobot(Graphics g)
                  {
                      Graphics grfx = g;
                      Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
          
                      int RobotOriginX = (this.ClientSize.Width / 2); // -(RobotellipseWidth / 2);
                      int RobotOriginY = (this.ClientSize.Height / 2); // -(RobotellipseHeight / 2);
          
                      int RobotellipseWidth = 20;
                      int RobotellipseHeight = 20;
          
                      Graphics grphx = this.CreateGraphics();
          
                      //Draw RobotElipse
                      for (int n = 0; n < 5; n++)
                      {
                          grphx.DrawEllipse(RobotEllipsePen,
                          RobotOriginX - (RobotellipseWidth / 2),
                          RobotOriginY - (RobotellipseHeight / 2),
                          (RobotellipseWidth + n),
                          (RobotellipseHeight + n));
                          RobotellipseWidth = RobotellipseWidth + 20;
                          RobotellipseHeight = RobotellipseHeight + 20;
                      }
                  }
          
                  protected override void OnPaint(PaintEventArgs e)
                  {
                      // drawRobot(e.Graphics);
                  }
                  private void pictureBox1_OnPaint(object sender, PaintEventArgs e)
                  {
                      drawRobot(e.Graphics);
                  }  
              }
          }

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            I can't see where you've attached to the event... it's actually the Paint event on a PictureBox. Have it set to your pictureBox1_OnP aint method, it should work. I've got similar going on my end and experiencing no troubles.

            Comment

            • mrcw
              New Member
              • Nov 2008
              • 82

              #7
              haven't I done that with the last method

              drawRobot(e.Gra phics);?

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                No it still needs to be called. You've got a method there that does nothing. You need to have it attach to the paint method of the picturebox. You can either do it via the designer, or...

                Code:
                pictureBox1.Paint += new PaintEventHandler(YOUR METHOD NAME HERE);
                ... though it's probably easier to do via designer.

                Comment

                • mrcw
                  New Member
                  • Nov 2008
                  • 82

                  #9
                  I seem to be going backwards. I have re-done the form again, but the drawing is still in the form not in the paintbox. I am using the picturebox paint event. The picturebox paint event appears to be attached to the right place in the designer.

                  The drawrobot method has gone
                  Code:
                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Data;
                  using System.Drawing;
                  using System.Linq;
                  using System.Text;
                  using System.Windows.Forms;
                  
                  namespace picbox_test1
                  {
                      public partial class Form1 : Form
                      {
                          public Form1()
                          {
                              InitializeComponent();
                          }
                  
                          private void pictureBox1_Paint(object sender, PaintEventArgs e)
                          {
                             // Graphics grfx = g;
                              Pen RobotEllipsePen = new Pen(System.Drawing.Color.Red);
                  
                              int RobotOriginX = (this.ClientSize.Width / 2); 
                              int RobotOriginY = (this.ClientSize.Height / 2); 
                  
                              int RobotellipseWidth = 20;
                              int RobotellipseHeight = 20;
                  
                              Graphics grphx = this.CreateGraphics();
                  
                              //Draw RobotElipse
                              for (int n = 0; n < 5; n++)
                              {
                                  grphx.DrawEllipse(RobotEllipsePen,
                                  RobotOriginX - (RobotellipseWidth / 2),
                                  RobotOriginY - (RobotellipseHeight / 2),
                                  (RobotellipseWidth + n),
                                  (RobotellipseHeight + n));
                                  RobotellipseWidth = RobotellipseWidth + 20;
                                  RobotellipseHeight = RobotellipseHeight + 20;
                              }
                          }
                      }
                  }
                  the designer code is
                  Code:
                  namespace picbox_test1
                  {
                      partial class Form1
                      {
                          /// <summary>
                          /// Required designer variable.
                          /// </summary>
                          private System.ComponentModel.IContainer components = null;
                  
                          /// <summary>
                          /// Clean up any resources being used.
                          /// </summary>
                          /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
                          protected override void Dispose(bool disposing)
                          {
                              if (disposing && (components != null))
                              {
                                  components.Dispose();
                              }
                              base.Dispose(disposing);
                          }
                  
                          #region Windows Form Designer generated code
                  
                          private void InitializeComponent()
                          {
                              this.pictureBox1 = new System.Windows.Forms.PictureBox();
                              ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                              this.SuspendLayout();
                              // 
                              // pictureBox1
                              // 
                              this.pictureBox1.Location = new System.Drawing.Point(38, 158);
                              this.pictureBox1.Name = "pictureBox1";
                              this.pictureBox1.Size = new System.Drawing.Size(100, 50);
                              this.pictureBox1.TabIndex = 0;
                              this.pictureBox1.TabStop = false;
                              this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
                              // 
                              // Form1
                              // 
                              this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
                              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                              this.ClientSize = new System.Drawing.Size(282, 255);
                              this.Controls.Add(this.pictureBox1);
                              this.Name = "Form1";
                              this.Text = "Form1";
                              ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                              this.ResumeLayout(false);
                  
                          }
                          #endregion
                  
                          private System.Windows.Forms.PictureBox pictureBox1;
                      }
                  }
                  I'm totally lost now I can't see what I'm doing wrong.

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    I tried your code, it didn't work at all and it's because you have made a mistake on line 30 of your paint method.

                    Code:
                    Graphics grphx = this.CreateGraphics();
                    ... should be ...

                    Code:
                    Graphics grphx = e.Graphics;
                    Other than that, it works fine. If it looks like things are drawing on the form, it's only because you don't have an image there. It's actually drawing on the picture box.

                    Comment

                    • mrcw
                      New Member
                      • Nov 2008
                      • 82

                      #11
                      I put a .jpg in the pictureBox using
                      Code:
                      pictureBox1.Image = Image.FromFile("c:/Users/m/Desktop/map-blank.jpg");
                      in the Form1_load event and the stuff I've drawn still disappears behind the picturebox

                      Comment

                      • GaryTexmo
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1501

                        #12
                        I just tested again, it's working for me. The drawing comes up over top of the image. This is with the code you posted, other than the correction I pointed out in my last post. Have you corrected that line in yours?

                        Comment

                        • mrcw
                          New Member
                          • Nov 2008
                          • 82

                          #13
                          yes I corrected my code. I have the pictureBpx over half the form1 and the robot graphics seem to be on the form1 background, The picturebox makes half of the robot graphic disappear. Also if I move the picturebox the robot stays where it is even it's not anywhere near the picturebox. Surely it should move when I move the pictureBox?

                          Comment

                          • GaryTexmo
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1501

                            #14
                            The reason for that is because of where you're drawing it. Look at lines 24 and 25 in your code. You're defining the origin as the center of the form, but when you're drawing on a control the origin is the top-left of that control, which is (0, 0). If your form size is (300, 300), your origin gets defined as (150, 150). If your picturebox is (50, 50) in size, then you're not going to see the whole picture because you're drawing in the bottom right of the picture box, outside its bounds.

                            As for it moving when you move the picturebox... I've copied and pasted your code into a project and it is doing exactly that. If yours isn't, you need to look over your code again and make sure you're drawing in the right place.

                            Remember, when you're drawing on a control the coordinate (0, 0) will always be in the top-left of the control you're drawing on.

                            Comment

                            • mrcw
                              New Member
                              • Nov 2008
                              • 82

                              #15
                              lines 24 and 25 should ensure the robot is drawn at the centre of the pictureBox. But it is drawn in the centre of the form. I have got a 700 x 700 form with the paintbox 200 x 200 at location 350, 420. The pictureBox is nowhere near the pictureBox, yet it still shows, yet according to you it should be out of the pictureBox bounds.

                              Comment

                              Working...