Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. drawing multi line

drawing multi line

Scheduled Pinned Locked Moved C#
graphicshelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mgroses
    wrote on last edited by
    #1

    I try to draw line with this code on a panel (I sent code only related to drawing). I can draw one line but if I want to draw multi line, there is an error like this: ArrgumentException was unhandled, Parameter is not valid. Where is my fault?

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

            if (drawLine == true)
            {
            PictureBox PictureBox2 = new PictureBox();
            int x1 = panel1.Width / 2;
            int y1 = panel1.Height / 2;
            PictureBox2.Location = new Point(x1, y1);
            PictureBox2.Size = new System.Drawing.Size(50, 30);
            PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
            panel1.Controls.Add(PictureBox2);
                
            PictureBox2.Image = Image.FromFile(@"C:\\warehouse.bmp");
    
    
           
                for (int i = 0; i < MaxRows; i++)
                {
                    
                    PictureBox PictureBox1 = new PictureBox();
                    PictureBox1.Image = Image.FromFile(@"C:\\retailer.bmp");
                    int x = coordinates\[i, 0\];
                    int y = coordinates\[i, 1\];
    
                    PictureBox1.Size = new System.Drawing.Size(25, 25);
                    PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    PictureBox1.Location = new Point(x1 + x, y1 - y);
                    panel1.Controls.Add(PictureBox1);
                    
                    Graphics g = e.Graphics;
                    Pen myPen = new Pen(Color.Black, 1);
                    g.DrawLine(myPen, x1, y1, x1+x, y1-y);
                    myPen.Dispose();
                    g.Dispose();
                   
                }
            }   
        }
    
    
    
        private void button1\_Click(object sender, EventArgs e)
        {
            TakeValues();
                
            drawLine = true;
            panel1.Invalidate();
        }
    
    H T 2 Replies Last reply
    0
    • M mgroses

      I try to draw line with this code on a panel (I sent code only related to drawing). I can draw one line but if I want to draw multi line, there is an error like this: ArrgumentException was unhandled, Parameter is not valid. Where is my fault?

      private void panel1_Paint(object sender, PaintEventArgs e)
      {

              if (drawLine == true)
              {
              PictureBox PictureBox2 = new PictureBox();
              int x1 = panel1.Width / 2;
              int y1 = panel1.Height / 2;
              PictureBox2.Location = new Point(x1, y1);
              PictureBox2.Size = new System.Drawing.Size(50, 30);
              PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
              panel1.Controls.Add(PictureBox2);
                  
              PictureBox2.Image = Image.FromFile(@"C:\\warehouse.bmp");
      
      
             
                  for (int i = 0; i < MaxRows; i++)
                  {
                      
                      PictureBox PictureBox1 = new PictureBox();
                      PictureBox1.Image = Image.FromFile(@"C:\\retailer.bmp");
                      int x = coordinates\[i, 0\];
                      int y = coordinates\[i, 1\];
      
                      PictureBox1.Size = new System.Drawing.Size(25, 25);
                      PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                      PictureBox1.Location = new Point(x1 + x, y1 - y);
                      panel1.Controls.Add(PictureBox1);
                      
                      Graphics g = e.Graphics;
                      Pen myPen = new Pen(Color.Black, 1);
                      g.DrawLine(myPen, x1, y1, x1+x, y1-y);
                      myPen.Dispose();
                      g.Dispose();
                     
                  }
              }   
          }
      
      
      
          private void button1\_Click(object sender, EventArgs e)
          {
              TakeValues();
                  
              drawLine = true;
              panel1.Invalidate();
          }
      
      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      There are several problems with the code that you have posted. 1) you should be aware that panel1_Paint happens every time that something happens to your form, which can be several times a second. 2) because of 1) you are creating two new PictureBoxes (PictureBox2 and PictureBox1) several times a second. This will make your code very, very slow if MaxRows is a large number. Since PictureBox2 is always in the same place, always the same size and always contains the same image why not create it once in the Form.Load() event handler, or better than that in the designer. If you are worried about it being in the correct place when your Form is resized, use the Anchor property. The parameter not valid refers to the Graphics object(g). Delete the g.Dispose() entirely. YOU DIDN'T CREATE IT, THEREFORE YOU MUSTN'T DISPOSE IT. You could also move the Pen myPen = new Pen(Color.Black, 1); outside the for loop, put it on the line above the for statement, and move the myPen.Dispose() call to after the closing brace of the for loop. No need to create it and then destroy it each time through the loop. Create it before the loop starts, then destroy it after the loop completes. [Edit] I have just realized that the problems with your code are worse than I noticed after my first brief reading of it. this section of code, from inside the for loop:

       PictureBox PictureBox1 = new PictureBox();
       PictureBox1.Image = Image.FromFile(@"C:\\retailer.bmp");
       ...........
       ...........
       ...........
       ...........
       PictureBox1.Location = new Point(x1 + x, y1 - y);
       panel1.Controls.Add(PictureBox1);         //<=========================== PB is added to panel1s Controls Collection
      

      means that the first time your Form is painted (assuming that MaxRows is 10, just for discussiion), there will be 11 PictureBoxes in panel1. The next time it is painted there will be 22 PictureBoxes stacked 2 deep in the same locations, the next time 33 PictureBoxes and so on. All stacked one on top of the other. You can mitigate this partly by turning off drawLine as the last statement of the:

              if (drawLine == true)
              {
                PictureBox PictureBox2 = new PictureBox();
                int x1 = panel1.Width / 2;
                int y1 =
      
      M 1 Reply Last reply
      0
      • H Henry Minute

        There are several problems with the code that you have posted. 1) you should be aware that panel1_Paint happens every time that something happens to your form, which can be several times a second. 2) because of 1) you are creating two new PictureBoxes (PictureBox2 and PictureBox1) several times a second. This will make your code very, very slow if MaxRows is a large number. Since PictureBox2 is always in the same place, always the same size and always contains the same image why not create it once in the Form.Load() event handler, or better than that in the designer. If you are worried about it being in the correct place when your Form is resized, use the Anchor property. The parameter not valid refers to the Graphics object(g). Delete the g.Dispose() entirely. YOU DIDN'T CREATE IT, THEREFORE YOU MUSTN'T DISPOSE IT. You could also move the Pen myPen = new Pen(Color.Black, 1); outside the for loop, put it on the line above the for statement, and move the myPen.Dispose() call to after the closing brace of the for loop. No need to create it and then destroy it each time through the loop. Create it before the loop starts, then destroy it after the loop completes. [Edit] I have just realized that the problems with your code are worse than I noticed after my first brief reading of it. this section of code, from inside the for loop:

         PictureBox PictureBox1 = new PictureBox();
         PictureBox1.Image = Image.FromFile(@"C:\\retailer.bmp");
         ...........
         ...........
         ...........
         ...........
         PictureBox1.Location = new Point(x1 + x, y1 - y);
         panel1.Controls.Add(PictureBox1);         //<=========================== PB is added to panel1s Controls Collection
        

        means that the first time your Form is painted (assuming that MaxRows is 10, just for discussiion), there will be 11 PictureBoxes in panel1. The next time it is painted there will be 22 PictureBoxes stacked 2 deep in the same locations, the next time 33 PictureBoxes and so on. All stacked one on top of the other. You can mitigate this partly by turning off drawLine as the last statement of the:

                if (drawLine == true)
                {
                  PictureBox PictureBox2 = new PictureBox();
                  int x1 = panel1.Width / 2;
                  int y1 =
        
        M Offline
        M Offline
        mgroses
        wrote on last edited by
        #3

        Thanks for your help. I started programing only one month ago. I have done many mistake like this. But I try to learn. Thanks again. I checked your code. Really I want to thanks you again. I will draw the images without using picturebox. However, My problem still go on. Yes, all lines is drawn after button click, but If I minimize the form and maximize again, all lines disappear. Do you have any idea about it? How can I solve this problem?

        modified on Friday, August 21, 2009 4:42 PM

        H 1 Reply Last reply
        0
        • M mgroses

          Thanks for your help. I started programing only one month ago. I have done many mistake like this. But I try to learn. Thanks again. I checked your code. Really I want to thanks you again. I will draw the images without using picturebox. However, My problem still go on. Yes, all lines is drawn after button click, but If I minimize the form and maximize again, all lines disappear. Do you have any idea about it? How can I solve this problem?

          modified on Friday, August 21, 2009 4:42 PM

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          mgroses wrote:

          If I minimize the form and maximize again, all lines disappear. Do you have any idea about it? How can I solve this problem

          This is because in the panel1_Paint event handler you only draw the lines if drawLine is true. When the Panel is resized drawLine is still false. One way to solve this is to handle the Form.Resize event like this:

          private void Form1\_Resize(object sender, EventArgs e)
          {
              drawLine = true;
          }
          

          but really this is just patching one hole at a time. Get another program running at the same time as your application. Make sure that its window is not maximised and then move it so that it partly covers the images and lines in your application and then move it away. Are your lines redrawn? How are you going to cure that? The problem is that the design of your program is wrong. You need to arrange things so that everything is drawn every time the panel repaints. There are too many ways of doing that to give them here. Why don't you have a think about it, have a go and come back if you have any problems.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          M 1 Reply Last reply
          0
          • H Henry Minute

            mgroses wrote:

            If I minimize the form and maximize again, all lines disappear. Do you have any idea about it? How can I solve this problem

            This is because in the panel1_Paint event handler you only draw the lines if drawLine is true. When the Panel is resized drawLine is still false. One way to solve this is to handle the Form.Resize event like this:

            private void Form1\_Resize(object sender, EventArgs e)
            {
                drawLine = true;
            }
            

            but really this is just patching one hole at a time. Get another program running at the same time as your application. Make sure that its window is not maximised and then move it so that it partly covers the images and lines in your application and then move it away. Are your lines redrawn? How are you going to cure that? The problem is that the design of your program is wrong. You need to arrange things so that everything is drawn every time the panel repaints. There are too many ways of doing that to give them here. Why don't you have a think about it, have a go and come back if you have any problems.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            M Offline
            M Offline
            mgroses
            wrote on last edited by
            #5

            thnaks for your help. This is not my real project. I changed my codes. I didn't use picturebox(I used drawimage function). and I have no problem about lines now. You are right I have to think about the design of the program. I try to do step by step. If I have any problem, I will contact.

            1 Reply Last reply
            0
            • M mgroses

              I try to draw line with this code on a panel (I sent code only related to drawing). I can draw one line but if I want to draw multi line, there is an error like this: ArrgumentException was unhandled, Parameter is not valid. Where is my fault?

              private void panel1_Paint(object sender, PaintEventArgs e)
              {

                      if (drawLine == true)
                      {
                      PictureBox PictureBox2 = new PictureBox();
                      int x1 = panel1.Width / 2;
                      int y1 = panel1.Height / 2;
                      PictureBox2.Location = new Point(x1, y1);
                      PictureBox2.Size = new System.Drawing.Size(50, 30);
                      PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
                      panel1.Controls.Add(PictureBox2);
                          
                      PictureBox2.Image = Image.FromFile(@"C:\\warehouse.bmp");
              
              
                     
                          for (int i = 0; i < MaxRows; i++)
                          {
                              
                              PictureBox PictureBox1 = new PictureBox();
                              PictureBox1.Image = Image.FromFile(@"C:\\retailer.bmp");
                              int x = coordinates\[i, 0\];
                              int y = coordinates\[i, 1\];
              
                              PictureBox1.Size = new System.Drawing.Size(25, 25);
                              PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                              PictureBox1.Location = new Point(x1 + x, y1 - y);
                              panel1.Controls.Add(PictureBox1);
                              
                              Graphics g = e.Graphics;
                              Pen myPen = new Pen(Color.Black, 1);
                              g.DrawLine(myPen, x1, y1, x1+x, y1-y);
                              myPen.Dispose();
                              g.Dispose();
                             
                          }
                      }   
                  }
              
              
              
                  private void button1\_Click(object sender, EventArgs e)
                  {
                      TakeValues();
                          
                      drawLine = true;
                      panel1.Invalidate();
                  }
              
              T Offline
              T Offline
              TAFIN
              wrote on last edited by
              #6

              As much i know that there is a method "protected void OnPaint(eventArgs e)" is a function that can be used for your problem. try it!! and let me know. thanks Mahbub-E-Rabbani [TAFIN] :rolleyes:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups