473,840 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# GDI+ line rendering problem...

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.imag e = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do e.Graphics.Draw Image
(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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
7 3231
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.PixelFo rmat.Format24bp pRgb);

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.FromIm age(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.imag e = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do e.Graphics.Draw Image
(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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
That makes perfect sense Mark, thanks ! :) I'll give it a go tomorrow.

Regards,

Todd.
"Mark R. Dawson" <Ma*********@di scussions.micro soft.com> wrote in message
news:D7******** *************** ***********@mic rosoft.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.PixelFo rmat.Format24bp pRgb);

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.FromIm age(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.imag e = mybitmap. Ive created my own class, inheriting from
the
picturebox and overriding the OnPaint event so I can do
e.Graphics.Draw Image
(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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
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.FromIm age 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.p ol.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.imag e = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do
e.Graphics.Draw Image (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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
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.p ol.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.imag e = mybitmap. Ive created my own class, inheriting from the
picturebox and overriding the OnPaint event so I can do
e.Graphics.Draw Image (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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
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************ ****@frontierne t.net> wrote in message
news:64******** *********@news0 1.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.FromIm age 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.p ol.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.imag e = mybitmap. Ive created my own class, inheriting from
the picturebox and overriding the OnPaint event so I can do
e.Graphics.Draw Image (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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
err, thanks for your reply Bob.

Todd.

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:ut******** ******@tk2msftn gp13.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.p ol.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.imag e = mybitmap. Ive created my own class, inheriting from
the picturebox and overriding the OnPaint event so I can do
e.Graphics.Draw Image (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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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
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*********@di scussions.micro soft.com> wrote in message
news:D7******** *************** ***********@mic rosoft.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.PixelFo rmat.Format24bp pRgb);

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.FromIm age(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.imag e = mybitmap. Ive created my own class, inheriting from
the
picturebox and overriding the OnPaint event so I can do
e.Graphics.Draw Image
(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.Crea teGraphics ();
myGraphic.DrawL ine (....);
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
15668
by: felix | last post by:
Hi, I tried google and searched the old posts -- with no success. Like a zillion of people before I have a problem when rendering transparent PNGs with GDI+ and the C# Graphics class. The image shows am object on a transparent background. I get ugly artifacts at the object's edge where the transparency begins. It looks as if there was a 8bit alpha reduced to 1 bit. Where there wasn't full transparency, the background (I suppose blue)...
6
4063
by: James dean | last post by:
I have heard that the video drivers in GDI+ are a big performance issue. But is this only an issue with something like Games Programming i think...is this wrong?. What about a drawing application just drawing simple one dimensional shapes(Polygons,beziers,polylines etc...) are these video drivers still useful for this?. If the video drivers are not a problem then is there any other performance problems that would be disastrous for a...
1
5343
by: James dean | last post by:
Could someone explain how this works. I think the graphics card is used to do blitting and drawing shapes like rectangles. How does it draw using the Graphics card on the PC and why is this feature left out of GDI+?. *** Sent via Developersdex http://www.developersdex.com ***
6
6284
by: James dean | last post by:
I want a good site that will show clearly how much more functionality GDI+ has. I cannot seem to find anything other than sites that list "some" of the new functionality that GDI+ offers. A comprehensive list would be great.... *** Sent via Developersdex http://www.developersdex.com ***
2
5386
by: Zanna | last post by:
Hi all! I'm in difficulty with this: I need to know the height of a text line that is written with Graphics.DrawString(). In theory I need just to get the Graphics.MeasureString() result. But this works if the string is on a single line. If the string goes on more lines (caused by a new line or word wrap), it seems that I need to compute the space between the lines, since the height
3
2838
by: brianbasquille | last post by:
Hello all, Haven't been here in a while but undertaking a project where i'm branching out the (presumingly abandoned) open-source OpenHTPC (www.openhtpc.org) with additional Personal Video Recorder functionality to it - meaning recording from TV / browsing TV listings etc. Am just working on rendering the XML TV Listings using GDI+. I already have the TV listings downloaded and contained in individual XML files
7
5979
by: Marcin Rzeznicki | last post by:
Hello, Do you think it is legitimate practice to mix GDI+ and GDI calls (via Get/ReleaseHDC()) in paint event of a control? I've heard there is possibility of performance loss while "locking" Graphics object which is done as a side-effect to GetHDC() call - could you confirm? Another question that comes to my mind when planning mentioned operation is: if the control painted on uses double-buffering style, will GDI calls make use of "back...
7
1730
by: =?Utf-8?B?U2ltb24gVGFtbWFu?= | last post by:
I was trying to double buffer a control while drawing on CE devices. The code below is compiled under the compact framework but you can also run it on the desktop and it produces the same problem. The basic problem is that my controls don't seem to correctly process an invalidate call after a window is dropped on top on them, mainly the "cheat" windows such as a combobox drop down or a loading symbol or volume control (which you get in...
7
2541
by: =?Utf-8?B?Um9oaXQ=?= | last post by:
I have a timer object that calls UpdateWindow(). The WM_TIMER message is processed in the main Win32 message loop for the window. However, when I run the app, the image doesn't get updated (there is some animation that's supposed to happen). However, if I constantly resize the window, the animation happens as expected (but I want the animation to happen without me having to mess with the window size). I verified that the timer is firing...
0
9856
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
9698
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10916
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
10657
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
9436
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
7022
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
5684
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...
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.