Creatig ellipse on a picturebox and making ellipse a link
-
Hey im back with a new questions / problems ;) Question 1: I have a image that is in a picturebox which is called pictureBox1 I want to read monstersetbase.txt and add small ellipses to my image. The position of the ellipse comes from my monstersetbase.txt. The image is 256x256 and the coordinates is example 125x150. I figured out a way to write a ellipse on the picturebox. It looks like this:
public void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { System.Drawing.Graphics g = e.Graphics; g.DrawEllipse(System.Drawing.Pens.Red, 125, 150, 2, 2); }
And on my form load:public void Form1_Load(object sender, EventArgs e) { pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint); this.Controls.Add(pictureBox1); }
Now my problem is that I cant seem to figure out how to read the txt file properly when I need all the data If I use for example:public void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { String line; String data; System.Drawing.Graphics g = e.Graphics; // Reading txt file from fileLocation using (StreamReader sr = new StreamReader(Convert.ToString(@fileLocation))) { while ((line = sr.ReadLine()) != null) { // Converting line to string from txt file data = Convert.ToString(line); // Splitting data into piecesMap[] String[] piecesMap = data.Split('\t'); // Converting x/y cord to float from string float xCord = float.Parse(piecesMap[3]); float yCord = float.Parse(piecesMap[4]); // Displaying ellipse g.DrawEllipse(System.Drawing.Pens.Red, xCord, yCord, 2, 2); } } }
then im getting the error: Index was outside the bounds of the array. Any suggestions? My monstersetbase.txt:3 0 0 30 78 60 240 240 -1 5 // Red Dragon 1 0 30 185 132 185 132 -1 10 // Golden Titan 2 0 30 183 128 188 135 -1 10 // Golden Solider 3 0 30 10 10 240 240 -1 12 // Golden Goblin 4 0 30 10 10 240 240 -1 8 // Golden Dragon 5 0 30 185 132 185 132 -1 7 // Golden Lizard King 6 0 30 132 83 132 83 -1 10 // Golden Vepar 7 0 30 183 128 188 135 -1 6 // Golden Tantalos 8 0 30 132 83 138 90 -1 10 // Golden Wheel end 1 9 33 10 85 162 95 168 -1 5 // Kundun Demon 10 29 30 40 113 45 116 -1 3 // Kundum Demon 11 41 5 126 160 125 161 -1 2 // Kundum Demon 12 38 5 106 161 111 160 -1 2 // Kundum Demon end
78 and 60 is the x and y coo -
Hey im back with a new questions / problems ;) Question 1: I have a image that is in a picturebox which is called pictureBox1 I want to read monstersetbase.txt and add small ellipses to my image. The position of the ellipse comes from my monstersetbase.txt. The image is 256x256 and the coordinates is example 125x150. I figured out a way to write a ellipse on the picturebox. It looks like this:
public void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { System.Drawing.Graphics g = e.Graphics; g.DrawEllipse(System.Drawing.Pens.Red, 125, 150, 2, 2); }
And on my form load:public void Form1_Load(object sender, EventArgs e) { pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint); this.Controls.Add(pictureBox1); }
Now my problem is that I cant seem to figure out how to read the txt file properly when I need all the data If I use for example:public void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { String line; String data; System.Drawing.Graphics g = e.Graphics; // Reading txt file from fileLocation using (StreamReader sr = new StreamReader(Convert.ToString(@fileLocation))) { while ((line = sr.ReadLine()) != null) { // Converting line to string from txt file data = Convert.ToString(line); // Splitting data into piecesMap[] String[] piecesMap = data.Split('\t'); // Converting x/y cord to float from string float xCord = float.Parse(piecesMap[3]); float yCord = float.Parse(piecesMap[4]); // Displaying ellipse g.DrawEllipse(System.Drawing.Pens.Red, xCord, yCord, 2, 2); } } }
then im getting the error: Index was outside the bounds of the array. Any suggestions? My monstersetbase.txt:3 0 0 30 78 60 240 240 -1 5 // Red Dragon 1 0 30 185 132 185 132 -1 10 // Golden Titan 2 0 30 183 128 188 135 -1 10 // Golden Solider 3 0 30 10 10 240 240 -1 12 // Golden Goblin 4 0 30 10 10 240 240 -1 8 // Golden Dragon 5 0 30 185 132 185 132 -1 7 // Golden Lizard King 6 0 30 132 83 132 83 -1 10 // Golden Vepar 7 0 30 183 128 188 135 -1 6 // Golden Tantalos 8 0 30 132 83 138 90 -1 10 // Golden Wheel end 1 9 33 10 85 162 95 168 -1 5 // Kundun Demon 10 29 30 40 113 45 116 -1 3 // Kundum Demon 11 41 5 126 160 125 161 -1 2 // Kundum Demon 12 38 5 106 161 111 160 -1 2 // Kundum Demon end
78 and 60 is the x and y cooWhy are you using float.parse on ints ? You have one line in each that doesn't have 4 chars, so it seems you should check your array for 4 elements first. That's always wise anyhow.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Why are you using float.parse on ints ? You have one line in each that doesn't have 4 chars, so it seems you should check your array for 4 elements first. That's always wise anyhow.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Thank you for the answear(s) Now I got it writing on the picturebox and when clicking on ellipse it shows a messagebox But it only works for the last ellipse because the variable gp changes <.<<br mode="hold" /> Any idea? Here is the code:
public void readFileToPaint() { string line; string data; string fileLocation = @"D:\msbEditor\monstersetbase.txt"; using (StreamReader sr = new StreamReader(Convert.ToString(@fileLocation))) { while ((line = sr.ReadLine()) != null) { // Get rid of any trailing spaces line = line.TrimEnd(); // if line is blank skip to next line if (line == "") continue; data = line; // don't need a conversion here as line is a string // Splitting data into piecesMap[] String[] piecesMap = data.Split('\t'); // if piecesMap only contains one element, skip to next line if (piecesMap.Length == 1) continue; Rectangle rect = new Rectangle(Convert.ToInt16(piecesMap[3]), Convert.ToInt16(piecesMap[4]), 4, 4); gp.AddEllipse(rect); } } } public void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.DrawPath(new Pen(Color.Red), gp); } private void Form1_Load(object sender, EventArgs e) { readFileToPaint(); pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint); this.Controls.Add(pictureBox1); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (gp.IsVisible(e.X, e.Y)) { MessageBox.Show(""); } }
-
Thank you for the answear(s) Now I got it writing on the picturebox and when clicking on ellipse it shows a messagebox But it only works for the last ellipse because the variable gp changes <.<<br mode="hold" /> Any idea? Here is the code:
public void readFileToPaint() { string line; string data; string fileLocation = @"D:\msbEditor\monstersetbase.txt"; using (StreamReader sr = new StreamReader(Convert.ToString(@fileLocation))) { while ((line = sr.ReadLine()) != null) { // Get rid of any trailing spaces line = line.TrimEnd(); // if line is blank skip to next line if (line == "") continue; data = line; // don't need a conversion here as line is a string // Splitting data into piecesMap[] String[] piecesMap = data.Split('\t'); // if piecesMap only contains one element, skip to next line if (piecesMap.Length == 1) continue; Rectangle rect = new Rectangle(Convert.ToInt16(piecesMap[3]), Convert.ToInt16(piecesMap[4]), 4, 4); gp.AddEllipse(rect); } } } public void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.DrawPath(new Pen(Color.Red), gp); } private void Form1_Load(object sender, EventArgs e) { readFileToPaint(); pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint); this.Controls.Add(pictureBox1); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (gp.IsVisible(e.X, e.Y)) { MessageBox.Show(""); } }
You need to keep a list of all the ellipses and iterate over them, instead of just keepin ghte last one
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
You need to keep a list of all the ellipses and iterate over them, instead of just keepin ghte last one
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Any idea of that? I cant seem to think of a way :x gp is defined here: GraphicsPath gp = new GraphicsPath();