Help drawing a line

S

Steve Long

Hello, (total GDI newbie)
I'm having trouble drawing just a simple line to display in a picturebox. I
just want a straight, dotdash line.

I have two methods, one works and one doesn't (it cause an exception to be
thrown):

This one works but it's not the results I want.
private void CreateImage1()
{
Bitmap b = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(b);
g.DrawEllipse(Pens.Red, new RectangleF(0, 0, b.Width, b.Height));
pb1.Image = b;
g.Dispose();
}

This one cause the exception to be thrown:
private void CreateImage()
{
Bitmap bmp = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmp);
Pen p = new Pen(Color.AliceBlue, 2);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
Point p1 = new Point(pb1.Left);
Point p2 = new Point(pb1.Right);
g.DrawLine(p, p1, p2);
pb1.Image = bmp;
g.Dispose();
bmp.Dispose();
}

Anybody know what I'm having these problems?

I appreciate your help btw.
Steve
 
S

Steve Long

Bob,
Thanks for your response. That does indeed stop the exception from being
thrown but the bitmap still doesn't display. I'm calling the method in the
form_paint event. Any idea why I'm not seeing the image?

Steve
 
P

Peter Hauge [msft]

I would recommend using the PaintEventArgs object directly instead of
creating your own Graphics object and disposing of it... Putting the code
directy in the paint method, the code would look like:

private void pb1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
Pen p = new Pen(Color.AliceBlue, 2);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
Point p1 = new Point(pb1.Left);
Point p2 = new Point(pb1.Right);
e.Graphics.DrawLine(p, p1, p2);
}

If you want a separate method to draw the line, just pass in the
PaintEventArgs, or the Graphics object to the method, "CreateImage".

Some MSDN articles that might help:

Good luck!
Pete Hauge, Visual C# Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

--------------------
From: "Steve Long" <[email protected]>
References: <[email protected]>
 
P

Peter Hauge [msft]

Sorry -- missed the MSDN articles... Here they are:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/
vclrfcodedrawinglineonformvisualc.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdrawinggraphicsclassdrawlinestopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemDrawingGraphicsClassTopic.asp

Good examples for programming with the graphics object:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscon/html/
vcsorigraphicsprogrammingexampletopics.asp

Pete Hauge, Visual C# Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

--------------------
 
S

Steve Long

Peter,
Thanks much for your helpful post. The line is still not being displayed
however. It's like the line is being painted and then it disappears or
something. Any ideas why? Is my system all messed up or something? I pasted
your code directly into the paint event of pb1.

Steve
 
S

Steve Long

Peter,
I got a bitmap to display in the picturebox using the last link's example
from this list. Thanks again for your help.

Steve
 
B

Bob Powell [MVP]

Hmm Form Paint is not the best place..

Why are you using a picturebox, is it because you think it's a useful place
to paint an image? PictureBox is just for displaying pictures.

Check this article. http://www.bobpowell.net/accursed_picturebox.htm I know
you're not comitting the cardinal sin but the code is just what you need.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
 
S

Steve Long

Bob,
I found your arcticles informative, and, in the process I discovered an
error in my logic. I was misplacing any lines I wanted shown by
miscalculating the x and y values. I found that using the panel control is
fine for creating (at least some of the) graphics that I need to create. I
have a mapping application for which I am creating a legend control that
shows the symbology of each layer for a map. In GIS (geographic information
systems) maps are map up of map layers and each layer has its own symbology.

Thanks for your post
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top