drawing multi line
-
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(); }
-
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(); }
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 newPictureBoxes (PictureBox2 and PictureBox1)
several times a second. This will make your code very, very slow ifMaxRows
is a large number. SincePictureBox2
is always in the same place, always the same size and always contains the same image why not create it once in theForm.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 theAnchor
property. The parameter not valid refers to theGraphics
object(g). Delete theg.Dispose()
entirely. YOU DIDN'T CREATE IT, THEREFORE YOU MUSTN'T DISPOSE IT. You could also move thePen myPen = new Pen(Color.Black, 1);
outside thefor
loop, put it on the line above the for statement, and move themyPen.Dispose()
call to after the closing brace of thefor
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 thefor
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 thatMaxRows
is 10, just for discussiion), there will be 11PictureBox
es in panel1. The next time it is painted there will be 22PictureBox
es stacked 2 deep in the same locations, the next time 33PictureBox
es 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 =
-
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 newPictureBoxes (PictureBox2 and PictureBox1)
several times a second. This will make your code very, very slow ifMaxRows
is a large number. SincePictureBox2
is always in the same place, always the same size and always contains the same image why not create it once in theForm.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 theAnchor
property. The parameter not valid refers to theGraphics
object(g). Delete theg.Dispose()
entirely. YOU DIDN'T CREATE IT, THEREFORE YOU MUSTN'T DISPOSE IT. You could also move thePen myPen = new Pen(Color.Black, 1);
outside thefor
loop, put it on the line above the for statement, and move themyPen.Dispose()
call to after the closing brace of thefor
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 thefor
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 thatMaxRows
is 10, just for discussiion), there will be 11PictureBox
es in panel1. The next time it is painted there will be 22PictureBox
es stacked 2 deep in the same locations, the next time 33PictureBox
es 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 =
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
-
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
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 ifdrawLine
is true. When thePanel
is resizeddrawLine
is still false. One way to solve this is to handle theForm.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.”
-
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 ifdrawLine
is true. When thePanel
is resizeddrawLine
is still false. One way to solve this is to handle theForm.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.”
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.
-
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(); }