帮助绘制一条线 [英] Help drawing a line
问题描述
您好,(总GDI新手)
我在绘制一个简单的线条以便在图片框中显示时遇到了麻烦。我只需要一个直线的dotdash线。
我有两种方法,一种是有效的,另外一种是不是(它会导致异常<抛出
抛出):
这个有效,但不是我想要的结果。
private void CreateImage1()
{
位图b =新位图(100,100);
图形g = Graphics.FromImage(b);
g.DrawEllipse(Pens.Red,new RectangleF(0,0,b.Width,b.Height));
pb1.Image = b;
g.Dispose();
}
这个导致抛出异常:
private void CreateImage()
{
位图bmp =新位图(100,100);
图形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();
}
任何人都知道我的意思我有这些问题吗?
我很感激你的帮助。
史蒂夫
在你的第二个例子结束时不要处理bmp。
-
Bob Powell [MVP]
C#,System.Drawing
9月份的Well Formed版本现已推出。
http://www.bobpowell.net/currentissue.htm
回答那些GDI +问题使用GDI +常见问题解答
http://www.bobpowell.net/gdiplus_faq。 htm
" Steve Long" <否*************** @ co.clark.wa.us>在消息中写道
news:eX ************** @ TK2MSFTNGP10.phx.gbl ...你好,(总GDI新手)
我在绘制一个简单的线条以显示在图片框中时遇到了麻烦。
我只想要一条直线的dotdash线。
我有两种方法,一种是有效的,另外一种是不是(它会导致异常被抛出):
这个有效,但不是我想要的结果。
private void CreateImage1()
{
位图b =新位图(100,100);
图形g = Graphics.FromImage(b);
g.DrawEllipse(Pens.Red,new RectangleF(0,0,b.Width,b.Height));
pb1.Image = b;
g.Dispose();
}
这个导致抛出异常:
private void CreateImage()
{
位图bmp =新位图(100,100);
图形g = Graphics.FromImage(bmp);
笔p =新笔(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();
}
Anybo我知道我遇到了这些问题吗?
我非常感谢你的帮助。
史蒂夫
在你的第二个例子结束时不要处理bmp。
-
Bob Powell [MVP]
C#,System.Drawing
九月的Well Formed版本现已推出。
http://www.bobpowell.net/currentissue.htm
回答那些GDI +问题GDI +常见问题解答
http://www.bobpowell.net/gdiplus_faq.htm
" Steve Long" <否*************** @ co.clark.wa.us>在消息中写道
news:eX ************** @ TK2MSFTNGP10.phx.gbl ...你好,(总GDI新手)
我在绘制一个简单的线条以显示在图片框中时遇到了麻烦。
我只想要一条直线的dotdash线。
我有两种方法,一种是有效的,另外一种是不是(它会导致异常被抛出):
这个有效,但不是我想要的结果。
private void CreateImage1()
{
位图b =新位图(100,100);
图形g = Graphics.FromImage(b);
g.DrawEllipse(Pens.Red,new RectangleF(0,0,b.Width,b.Height));
pb1.Image = b;
g.Dispose();
}
这个导致抛出异常:
private void CreateImage()
{
位图bmp =新位图(100,100);
图形g = Graphics.FromImage(bmp);
笔p =新笔(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();
}
Anybo我知道我遇到了这些问题吗?
我非常感谢你的帮助。
史蒂夫
Bob,
感谢您的回复。这确实可以阻止异常抛出
但是位图仍然没有显示。我在
form_paint事件中调用该方法。知道为什么我没有看到这张照片吗?
史蒂夫
" Bob Powell [MVP]" < bob@_spamkiller_bobpowell.net>在留言中写道
新闻:#W ************** @ TK2MSFTNGP10.phx.gbl ...不要在第二个例子结束时处理bmp。
- 鲍勃鲍威尔[MVP]
C#,System.Drawing
九月''现已推出s版本的Well Formed。
http://www.bobpowell .net / currentissue.htm
用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/gdiplus_faq.htm
Steve Long <否*************** @ co.clark.wa.us>在消息中写道
新闻:eX ************** @ TK2MSFTNGP10.phx.gbl ...你好,(总GDI新手)
图片框中时遇到了麻烦。我只是想要一条直线的dotdash线。 />
我有两个方法,一个工作,一个不会(它导致例外
被抛出):
<这个有效,但不是我想要的结果。
private void CreateImage1()
{
位图b =新位图(100,100);
图形g = Graphics.FromImage(b);
g.DrawEllipse(Pens.Red,new RectangleF(0,0,b.Width,b.Height));
pb1.Image = b;
g.Dispose();
}
这个导致抛出异常:
private void CreateImage()
{
位图bmp = new Bitmap(100,100);
图形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();
}
有人知道我有这些问题吗?
我很感激你的帮助。
史蒂夫
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
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
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
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.Ijust 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
这篇关于帮助绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!