473,856 Members | 1,773 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 2417
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
3224
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 allows the user to type 2-3 words in (on?) the
4
2017
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 code but I need help bad. How do i make this work? Thanks Mike
2
4918
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
2400
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 three weeks, just not enough to accomplish this one task. If anyone has a bit of free time and...
3
1347
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 System.Windows.Forms Public Class clsLoginScreen Inherits System.Windows.Forms.Form
3
1533
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 WITHIN a thread, based on what another THREAD is doing... here is the attached code... WHEN I click...
2
1926
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 files\root\d16a94c5\8b912d73\assembly\dl2\1dc1ef5c\006575a4_827ac301\metabuilders.webcontrols.rowselectorcolumn.dll"...
1
1236
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... Assignment Description Your second assignment will provide you with practice using static methods,...
5
12889
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
9916
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11056
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10782
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10384
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9531
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7093
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5761
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.