473,583 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FromIm age(b);
g.DrawEllipse(P ens.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.FromIm age(bmp);
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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 2401
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******** ******@TK2MSFTN GP10.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.FromIm age(b);
g.DrawEllipse(P ens.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.FromIm age(bmp);
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
g.DrawEllipse(P ens.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.FromIm age(bmp);
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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@_spamkille r_bobpowell.net > wrote in message
news:#W******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
g.DrawEllipse(P ens.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.FromIm age(bmp);
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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(objec t sender, System.Windows. Forms.PaintEven tArgs
e)
{
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.DashDot;
Point p1 = new Point(pb1.Left) ;
Point p2 = new Point(pb1.Right );
e.Graphics.Draw Line(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, "CreateImag e".

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@_spamkille r_bobpowell.net > wrote in message
news:#W******* *******@TK2MSFT NGP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
> g.DrawEllipse(P ens.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.FromIm age(bmp);
> Pen p = new Pen(Color.Alice Blue, 2);
> p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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/
vclrfcodedrawin glineonformvisu alc.asp
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdraw inggraphicsclas sdrawlinestopic .asp
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemDraw ingGraphicsClas sTopic.asp

Good examples for programming with the graphics object:
http://msdn.microsoft.com/library/de...us/cscon/html/
vcsorigraphicsp rogrammingexamp letopics.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(objec t sender, System.Windows. Forms.PaintEven tArgse)
{
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.DashDot;
Point p1 = new Point(pb1.Left) ;
Point p2 = new Point(pb1.Right );
e.Graphics.Draw Line(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, "CreateImag e".

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*********** ***@TK2MSFTNGP1 0.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@_spamkille r_bobpowell.net > wrote in message
news:#W****** ********@TK2MSF TNGP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
> g.DrawEllipse(P ens.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.FromIm age(bmp);
> Pen p = new Pen(Color.Alice Blue, 2);
> p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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********@onl ine.microsoft.c om> wrote in message
news:M7******** ******@cpmsftng xa06.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(objec t sender, System.Windows. Forms.PaintEven tArgs
e)
{
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.DashDot;
Point p1 = new Point(pb1.Left) ;
Point p2 = new Point(pb1.Right );
e.Graphics.Draw Line(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, "CreateImag e".

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@_spamkille r_bobpowell.net > wrote in message
news:#W******* *******@TK2MSFT NGP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
> g.DrawEllipse(P ens.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.FromIm age(bmp);
> Pen p = new Pen(Color.Alice Blue, 2);
> p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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********@onl ine.microsoft.c om> wrote in message
news:$#******** ******@cpmsftng xa06.phx.gbl...
Sorry -- missed the MSDN articles... Here they are:
http://msdn.microsoft.com/library/de...us/cscon/html/ vclrfcodedrawin glineonformvisu alc.asp
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemdraw inggraphicsclas sdrawlinestopic .asp
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemDraw ingGraphicsClas sTopic.asp

Good examples for programming with the graphics object:
http://msdn.microsoft.com/library/de...us/cscon/html/ vcsorigraphicsp rogrammingexamp letopics.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(objec t sender,

System.Windows. Forms.PaintEven tArgs
e)
{
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.DashDot;
Point p1 = new Point(pb1.Left) ;
Point p2 = new Point(pb1.Right );
e.Graphics.Draw Line(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, "CreateImag e".

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*********** ***@TK2MSFTNGP1 0.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@_spamkille r_bobpowell.net > wrote in message
news:#W****** ********@TK2MSF TNGP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
> g.DrawEllipse(P ens.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.FromIm age(bmp);
> Pen p = new Pen(Color.Alice Blue, 2);
> p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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******** ******@TK2MSFTN GP11.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@_spamkille r_bobpowell.net > wrote in message
news:#W******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
g.DrawEllipse(P ens.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.FromIm age(bmp);
Pen p = new Pen(Color.Alice Blue, 2);
p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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@_spamkille r_bobpowell.net > wrote in message
news:eZ******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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@_spamkille r_bobpowell.net > wrote in message
news:#W******** ******@TK2MSFTN GP10.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******** ******@TK2MSFTN GP10.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.FromIm age(b);
> g.DrawEllipse(P ens.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.FromIm age(bmp);
> Pen p = new Pen(Color.Alice Blue, 2);
> p.DashStyle = System.Drawing. Drawing2D.DashS tyle.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3213
by: ComputerSmith | last post by:
Hi all. I have programmed VB6 apps before, ones that use dropdown listboxes, text boxes, etc... normal stuff. I was asked by a friend to write a "simple" app that I am unsure how to proceed with. Any and all help is appreciated. 1) On start of the app, he wants a circle to appear in the middle of the screen. 2) Clicking the circle...
4
1997
by: Mike | last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am trying to implement a singleton class. Form 1 should create a user object and populate some properties in user. Form2 should then have access to those user properties. I am not getting the property value from user that was set in form1. Sorry for posting so much...
2
4902
by: Serge Klokov | last post by:
Hi! 1. Please, help with example "paint on form by mouse" 2. Below is my example, but it clear the line after each Refresh()... how to fix? 3. How to draw the line in Mouse_Move event? private Boolean isCanPaint = false; private MouseEventArgs MouseEvent1;
5
2379
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function correctly. I am trying to learn C# after playing around for a bit with procedural programming with PHP, not OOP, and believe I have learned quite a bit in...
3
1331
by: Chris Calzaretta | last post by:
I need to create a form from this web service http://24.163.239.122/wsprojecttrackerobjects/wsprojecttrackerobjects.asmx if you call getloginscreen there is a field called screendescription that field screendescription has the form code I am trying to create at runtime the code that is in the field screendecription is Imports...
3
1514
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be able to communicate with each other, either through a message loop, or some other manner. I ALSO need to be able to CALL specific functions / subs...
2
1911
by: momo | last post by:
Hello Guys, I have a bit of a problem, I created a Dll called SecureQueryStringDll.dll and I had the dll put bin folder of my application first and it did not work so I then put it in the bin folder of c:\inetpub\wwwroot and it still does not work. Here is the error I get from it: temporary asp.net...
1
1221
by: tkempy | last post by:
okay so i have a CIS class called abstraction and design. i'm completely new to this "java" stuff. I've never seen any of it in my life. our second homework is to create a program that produces a "ASCII art" version of the lincoln financial field where the eagles play. anyone who can help???? These are all the guidelines and what not... ...
5
12863
by: Macias | last post by:
Hi, Please help me, how I can make a line drawing on PictureBox similar to MS paint. I thinking about this: click and hold button, move mouse and relase button. I'm trying useing this g.DrawLine(..), but I have a lot of line. In C++ is a parameter XORPUT, but in C# I can't find it.
0
7893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8188
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6574
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3813
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.