By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

C# GDI+ line rendering problem...

P: n/a
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do e.Graphics.DrawImage
(this.image....). This renders correctly, without flicker and without
problems.

The problem comes with the graph... Im drawing the graph using GDI+ methods
with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two pictureboxes...
The image renders correctly but the graph will not and the popup window
erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and then,
in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
To braw a line on am image you need to use the graphics object, so for
example in the code below you create a new bitmap (you can use a pre-existing
image) and draw a line on it:

//Create a new bitmap to draw onto
Bitmap b = new
Bitmap(100,100,System.Drawing.Imaging.PixelFormat. Format24bppRgb);

Graphics g;
Pen p;
Point p1, p2;

try
{
//create graphics object that will allow us to draw on our bitmap
//notice how the bitmap is passed to the function
g = Graphics.FromImage(b);

//set up drawing object
p = new Pen(Color.Red);
p1 = new Point(0,0);
p2 = new Point(50,50);

//draw the line onto the bitmap
g.DrawLine(p, p1, p2);
}
finally
{
//important to make sure we tidy up
g.Dispose();
p.Dispose();
}
Now in your OnPaint method you just write the bitmap image to the control.

Hope that starts you in the right direction.

Mark.

"ne**@OxfordEye.co.uk" wrote:
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do e.Graphics.DrawImage
(this.image....). This renders correctly, without flicker and without
problems.

The problem comes with the graph... Im drawing the graph using GDI+ methods
with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two pictureboxes...
The image renders correctly but the graph will not and the popup window
erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and then,
in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd

Nov 17 '05 #2

P: n/a
That makes perfect sense Mark, thanks ! :) I'll give it a go tomorrow.

Regards,

Todd.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
To braw a line on am image you need to use the graphics object, so for
example in the code below you create a new bitmap (you can use a
pre-existing
image) and draw a line on it:

//Create a new bitmap to draw onto
Bitmap b = new
Bitmap(100,100,System.Drawing.Imaging.PixelFormat. Format24bppRgb);

Graphics g;
Pen p;
Point p1, p2;

try
{
//create graphics object that will allow us to draw on our bitmap
//notice how the bitmap is passed to the function
g = Graphics.FromImage(b);

//set up drawing object
p = new Pen(Color.Red);
p1 = new Point(0,0);
p2 = new Point(50,50);

//draw the line onto the bitmap
g.DrawLine(p, p1, p2);
}
finally
{
//important to make sure we tidy up
g.Dispose();
p.Dispose();
}
Now in your OnPaint method you just write the bitmap image to the control.

Hope that starts you in the right direction.

Mark.

"ne**@OxfordEye.co.uk" wrote:
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from
the
picturebox and overriding the OnPaint event so I can do
e.Graphics.DrawImage
(this.image....). This renders correctly, without flicker and without
problems.

The problem comes with the graph... Im drawing the graph using GDI+
methods
with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two
pictureboxes...
The image renders correctly but the graph will not and the popup window
erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and
then,
in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd

Nov 17 '05 #3

P: n/a
You can create a Graphics on any object that inherits from Image, including
Bitmap.
Consult:
http://msdn.microsoft.com/library/de...ctiontogdi.asp
which references the
Graphics.FromImage method.
Which is to say, you can use the GDI drawing routines with a Bitmap as the
underlying surface. Then you can copy that bitmap anywhere you wish.

On the other hand, it would seem that you could just redraw the graph
whenever the Paint event was raised on the window from which you called the
CreateGraphics method. I.E. put the logic that creates the Graphics, and
which draws the graph all in one method, and invoke that method in the
handler for the Paint event for the control that holds the graph. Then when
your popup moves off the graph's window, the Paint event will trigger the
redrawing of the graph. That is the usual way to handle the situation, I
believe. No Bitmap needed.
"ne**@OxfordEye.co.uk" <ne**@oxfordeye.co.uk> wrote in message
news:dd**********@newsg4.svr.pol.co.uk...
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do
e.Graphics.DrawImage (this.image....). This renders correctly, without
flicker and without problems.

The problem comes with the graph... Im drawing the graph using GDI+
methods with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two
pictureboxes... The image renders correctly but the graph will not and the
popup window erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and
then, in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd

Nov 17 '05 #4

P: n/a
Firstly, drawing the image property contents of a picture box to the picture
box is nonsensical because that's what it does anyway. If you need a custom
drawing solution for images inherit from control and lose the PictureBox
baggage.

Secondly, you should never render anthing using CreateGraphics. See the #1
most asked GDI+ FAQ question for why.

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

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

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

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"ne**@OxfordEye.co.uk" <ne**@oxfordeye.co.uk> wrote in message
news:dd**********@newsg4.svr.pol.co.uk...
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do
e.Graphics.DrawImage (this.image....). This renders correctly, without
flicker and without problems.

The problem comes with the graph... Im drawing the graph using GDI+
methods with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two
pictureboxes... The image renders correctly but the graph will not and the
popup window erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and
then, in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd

Nov 17 '05 #5

P: n/a
Hi Fred,

I just worry that it will be too slow to redraw the graph.

Ive solved it now using Marks suggestion above,

Thanks for your post

Todd.
"Fred Mellender" <no****************@frontiernet.net> wrote in message
news:64*****************@news01.roc.ny...
You can create a Graphics on any object that inherits from Image,
including Bitmap.
Consult:
http://msdn.microsoft.com/library/de...ctiontogdi.asp
which references the
Graphics.FromImage method.
Which is to say, you can use the GDI drawing routines with a Bitmap as the
underlying surface. Then you can copy that bitmap anywhere you wish.

On the other hand, it would seem that you could just redraw the graph
whenever the Paint event was raised on the window from which you called
the CreateGraphics method. I.E. put the logic that creates the Graphics,
and which draws the graph all in one method, and invoke that method in the
handler for the Paint event for the control that holds the graph. Then
when your popup moves off the graph's window, the Paint event will trigger
the redrawing of the graph. That is the usual way to handle the
situation, I believe. No Bitmap needed.
"ne**@OxfordEye.co.uk" <ne**@oxfordeye.co.uk> wrote in message
news:dd**********@newsg4.svr.pol.co.uk...
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from
the picturebox and overriding the OnPaint event so I can do
e.Graphics.DrawImage (this.image....). This renders correctly, without
flicker and without problems.

The problem comes with the graph... Im drawing the graph using GDI+
methods with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two
pictureboxes... The image renders correctly but the graph will not and
the popup window erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and
then, in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd


Nov 17 '05 #6

P: n/a
err, thanks for your reply Bob.

Todd.

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ut**************@tk2msftngp13.phx.gbl...
Firstly, drawing the image property contents of a picture box to the
picture box is nonsensical because that's what it does anyway. If you need
a custom drawing solution for images inherit from control and lose the
PictureBox baggage.

Secondly, you should never render anthing using CreateGraphics. See the #1
most asked GDI+ FAQ question for why.

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

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

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

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"ne**@OxfordEye.co.uk" <ne**@oxfordeye.co.uk> wrote in message
news:dd**********@newsg4.svr.pol.co.uk...
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from
the picturebox and overriding the OnPaint event so I can do
e.Graphics.DrawImage (this.image....). This renders correctly, without
flicker and without problems.

The problem comes with the graph... Im drawing the graph using GDI+
methods with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two
pictureboxes... The image renders correctly but the graph will not and
the popup window erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and
then, in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd


Nov 17 '05 #7

P: n/a
Yep, thanks Mark, got it working in under 5mins using your suggestion.

Im new to C# and the GDI+ and get lost in the functions sometimes...

Thanks again for the quick reply

Todd.
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
To braw a line on am image you need to use the graphics object, so for
example in the code below you create a new bitmap (you can use a
pre-existing
image) and draw a line on it:

//Create a new bitmap to draw onto
Bitmap b = new
Bitmap(100,100,System.Drawing.Imaging.PixelFormat. Format24bppRgb);

Graphics g;
Pen p;
Point p1, p2;

try
{
//create graphics object that will allow us to draw on our bitmap
//notice how the bitmap is passed to the function
g = Graphics.FromImage(b);

//set up drawing object
p = new Pen(Color.Red);
p1 = new Point(0,0);
p2 = new Point(50,50);

//draw the line onto the bitmap
g.DrawLine(p, p1, p2);
}
finally
{
//important to make sure we tidy up
g.Dispose();
p.Dispose();
}
Now in your OnPaint method you just write the bitmap image to the control.

Hope that starts you in the right direction.

Mark.

"ne**@OxfordEye.co.uk" wrote:
This may be a stupid question, but if I don't ask I'll never know ;)

Ok, here it goes.... I am writing an application that renders an image in
one picturebox and a graph in another.

The image is drawn by loading a jpg into a bitmap and then setting
picturebox.image = mybitmap. Ive created my own class, inheriting from
the
picturebox and overriding the OnPaint event so I can do
e.Graphics.DrawImage
(this.image....). This renders correctly, without flicker and without
problems.

The problem comes with the graph... Im drawing the graph using GDI+
methods
with :-

myGraphic = PictureBox.CreateGraphics ();
myGraphic.DrawLine (....);
etc.

I do this each time I believe the graph has changed.

The problem comes when I move a popup window across the two
pictureboxes...
The image renders correctly but the graph will not and the popup window
erases the graph below it.

Ideally, I'd like to render the graph in a similar way to the image but I
can't find how to draw a line in a bitmap - I could write a line drawing
function but that seems a little extreme ;)

Can anyone help ? I think I need to render the graph into a bitmap and
then,
in the OnPaint event, blit it to the screen... but how ?

Thanks for any help and sorry if this is obvious or stupid,

Todd

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.