472,145 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Help drawing a line

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
Nov 15 '05 #1
9 2228
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #3
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #4
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" <No***************@co.clark.wa.us>
References: <eX**************@TK2MSFTNGP10.phx.gbl> <#W**************@TK2MSFTNGP10.phx.gbl>Subject: Re: Help drawing a line
Date: Mon, 6 Oct 2003 08:41:23 -0700
Lines: 70

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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
> Hello, (total GDI newbie)
> I'm having trouble drawing just a simple line to display in apicturebox.
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
>
>




Nov 15 '05 #5
Sorry -- missed the MSDN articles... Here they are:
http://msdn.microsoft.com/library/de...us/cscon/html/
vclrfcodedrawinglineonformvisualc.asp
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdrawinggraphicsclassdrawlinestopic.asp
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemDrawingGraphicsClassTopic.asp

Good examples for programming with the graphics object:
http://msdn.microsoft.com/library/de...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

--------------------

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.PaintEventArgse)
{
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" <No***************@co.clark.wa.us>
References: <eX**************@TK2MSFTNGP10.phx.gbl>

<#W**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: Help drawing a line
Date: Mon, 6 Oct 2003 08:41:23 -0700
Lines: 70

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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
> 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
tobe
> 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
>
>



Nov 15 '05 #6
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

"Peter Hauge [msft]" <pe********@online.microsoft.com> wrote in message
news:M7**************@cpmsftngxa06.phx.gbl...
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" <No***************@co.clark.wa.us>
References: <eX**************@TK2MSFTNGP10.phx.gbl>

<#W**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: Help drawing a line
Date: Mon, 6 Oct 2003 08:41:23 -0700
Lines: 70

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 theform_paint event. Any idea why I'm not seeing the image?

Steve

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
> 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
tobe
> 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
>
>


Nov 15 '05 #7
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

"Peter Hauge [msft]" <pe********@online.microsoft.com> wrote in message
news:$#**************@cpmsftngxa06.phx.gbl...
Sorry -- missed the MSDN articles... Here they are:
http://msdn.microsoft.com/library/de...us/cscon/html/ vclrfcodedrawinglineonformvisualc.asp
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemdrawinggraphicsclassdrawlinestopic.asp
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemDrawingGraphicsClassTopic.asp

Good examples for programming with the graphics object:
http://msdn.microsoft.com/library/de...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

--------------------

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" <No***************@co.clark.wa.us>
References: <eX**************@TK2MSFTNGP10.phx.gbl>

<#W**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: Help drawing a line
Date: Mon, 6 Oct 2003 08:41:23 -0700
Lines: 70

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 theform_paint event. Any idea why I'm not seeing the image?

Steve

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
> 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

tobe
> 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
>
>


Nov 15 '05 #8
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eP**************@TK2MSFTNGP11.phx.gbl...
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #9
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
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eP**************@TK2MSFTNGP11.phx.gbl...
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

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:#W**************@TK2MSFTNGP10.phx.gbl...
Don't dispose of bmp at the end of your second example.

--
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

"Steve Long" <No***************@co.clark.wa.us> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Serge Klokov | last post: by
5 posts views Thread by MFC | last post: by
3 posts views Thread by Chris Calzaretta | last post: by
3 posts views Thread by Keith Mills | last post: by
2 posts views Thread by momo | last post: by
1 post views Thread by tkempy | last post: by
5 posts views Thread by Macias | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.