944,089 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 35627
  • C# RSS
Mar 17th, 2007
0

Graphing in PictureBox

Expand Post »
I have been trying to get a simple linear equation to draw. I can draw the axis fine but the equation will not draw. I think one would need a save/refresh function(s), but I don't know how to go about doing this:

C# Syntax (Toggle Plain Text)
  1. pw = pictureBox1.Size.Width;
  2. ph = pictureBox1.Size.Height;
  3. w = pictureBox1.Size.Width / 2;
  4. h = pictureBox1.Size.Height / 2;
  5. Graphics objGraphics = this.pictureBox1.CreateGraphics();
  6. Pen pen = new Pen(Color.Black);
  7. b = 2;
  8. m = 2;
  9. for (x = 0; x < 426; x++)
  10. {
  11. y = m * x + b;
  12. x = (y - b) / m;
  13. objGraphics.DrawLine(pen, x, y, x, y);
  14. System.Drawing.Drawing2D.GraphicsState graph = objGraphics.Save();
  15. objGraphics.Restore(graph);
  16. }
  17.  
  18.  
  19.  
  20. objGraphics.DrawLine(pen, 0, h, pw, h);
  21. objGraphics.DrawLine(pen, w, 0, w, ph);
Thanks for any help. I should mention that I did declare all variables (x, y, m, and b). I'm just starting out with VC#, so that might explain the sloppy or simple code.
Last edited by metkillerjoe; Mar 17th, 2007 at 2:14 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metkillerjoe is offline Offline
3 posts
since Mar 2007
Mar 18th, 2007
0

Re: Graphing in PictureBox

That is because you are plotting a point right on top of the original point.

I changed this line...
C# Syntax (Toggle Plain Text)
  1. objGraphic.DrawLine(pen, x, y, -x, -y);
Here is the code I used...
C# Syntax (Toggle Plain Text)
  1. int picBoxWidth = pictureBox1.Size.Width;
  2. int picBoxHeight = pictureBox1.Size.Height;
  3. int halfWidth = pictureBox1.Size.Width / 2;
  4. int halfHeight = pictureBox1.Size.Height / 2;
  5.  
  6. Graphics objGraphic = this.pictureBox1.CreateGraphics();
  7.  
  8. Pen pen = new Pen(Color.Black);
  9.  
  10. int b = 2;
  11. int m = 2;
  12. for (int x = 0; x < 426; x++)
  13. {
  14. int y = m * x + b;
  15. x = (y - b) / m;
  16. objGraphic.DrawLine(pen, x, y, -x, -y);
  17. System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
  18. objGraphic.Restore(graph);
  19. }
  20.  
  21. objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
  22. objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);
Hope that helps
Last edited by DDoSAttack; Mar 18th, 2007 at 3:37 am.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
DDoSAttack is offline Offline
15 posts
since Mar 2007
Mar 19th, 2007
0

Re: Graphing in PictureBox

Click to Expand / Collapse  Quote originally posted by DDoSAttack ...
That is because you are plotting a point right on top of the original point.

I changed this line...
C# Syntax (Toggle Plain Text)
  1. objGraphic.DrawLine(pen, x, y, -x, -y);
Here is the code I used...
C# Syntax (Toggle Plain Text)
  1. int picBoxWidth = pictureBox1.Size.Width;
  2. int picBoxHeight = pictureBox1.Size.Height;
  3. int halfWidth = pictureBox1.Size.Width / 2;
  4. int halfHeight = pictureBox1.Size.Height / 2;
  5.  
  6. Graphics objGraphic = this.pictureBox1.CreateGraphics();
  7.  
  8. Pen pen = new Pen(Color.Black);
  9.  
  10. int b = 2;
  11. int m = 2;
  12. for (int x = 0; x < 426; x++)
  13. {
  14. int y = m * x + b;
  15. x = (y - b) / m;
  16. objGraphic.DrawLine(pen, x, y, -x, -y);
  17. System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
  18. objGraphic.Restore(graph);
  19. }
  20.  
  21. objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
  22. objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);
Hope that helps
Thanks that did help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metkillerjoe is offline Offline
3 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to append string to string builder in C#
Next Thread in C# Forum Timeline: Store Locator with C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC