473,414 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

GDI+ Z-order?

Hi all,

I use VB.NET (C# would do the same) and GDI+ to draw on a form. I'd
like to know if it is possible to set the z-order when we draw
something...Let's consider that simplified case :

' Assume there is an 'items' array that contains information from which
I draw
' Also assume I have a valid Graphics object named g
Dim pts(5) As Point
Dim i as integer
For i = 0 to 4
g.DrawLine(Pens.LightGray, items(i).x1, items(i).y1, items(i).x2,
items(i).y2)
pts(i) = new Point(items(i).ptX, items(i).ptY)
Next
g.FillPolygon(Brushes.LightGreen, pts)

Now, the thing is, I'd like to draw the polygon UNDER the lines, which
is not the case right now, because it is rendered after...the only way
I find to do this right now is to do the same loop from 0 to 4 before
the current one, ONLY to construct the pts array...then after, draw the
polygon (so it's before the lines), and then loop within the current
For we see here, to draw lines...Of course, it's really ugly to make 2
loops just for that!! Is there a way I can draw the polygon under the
lines, while having only one loop, as in the current scenario?? Kind of
a z-order to set? How :S

thanks for all your help in advance! :)

Oct 26 '06 #1
5 3713
n!
Items are drawn in the order you supply them, there is no built in way to
sort your drawing instructions for you. However, you can change your code to
something else:

Dim pts( 5 ) As Point
Dim i as integer
For i = 0 to 4
pts(i) = new Point( items(i).ptX, items(i).ptY )
Next

g.FillPolygon( Brushes.LightGreen, pts );
g.DrawPolygon( Pens.LightGray, pts );

Apologies for any bad VB code, I've only used C# before ;o

n!
Oct 26 '06 #2
Hi,

thanks for your quick reply. I understand what you mean, but
unfortunately, the points for my polygon and for my lines are not the
same, a.k.a, I don't want to outline the polygon, as I would have used
drawpolygon after, just like you did :)

Here's a simple image that could help you visualize :
http://img174.imageshack.us/img174/6628/errrsw0.gif

So I need to build my pts array in order to be able to draw my polygon,
but while I loop for that, I thought 'why not draw the lines'...Now, I
end up with my polygon drawn after, because I can only draw it once I
have all the points. If I want to draw my lines after, do I really have
to add another loop? So one loop to build points, then draw polygon,
then loop again through the same data to draw lines?...I'd really like
to avoid looping twice, because let's say I have 20,000 items, that
double loop is starting to be really expensive...

thanks for any advice :)

n! wrote:
Items are drawn in the order you supply them, there is no built in way to
sort your drawing instructions for you. However, you can change your code to
something else:

Dim pts( 5 ) As Point
Dim i as integer
For i = 0 to 4
pts(i) = new Point( items(i).ptX, items(i).ptY )
Next

g.FillPolygon( Brushes.LightGreen, pts );
g.DrawPolygon( Pens.LightGray, pts );

Apologies for any bad VB code, I've only used C# before ;o

n!
Oct 26 '06 #3
n!
So I need to build my pts array in order to be able to draw my polygon,
but while I loop for that, I thought 'why not draw the lines'...Now, I
end up with my polygon drawn after, because I can only draw it once I
have all the points. If I want to draw my lines after, do I really have
to add another loop? So one loop to build points, then draw polygon,
then loop again through the same data to draw lines?...I'd really like
to avoid looping twice, because let's say I have 20,000 items, that
double loop is starting to be really expensive...
You can also build a list of lines along with the polygon, modifying the
original code:

Dim pts( 5 ) As Point
Dim lines( 5 ) As Point
Dim i as integer
For i = 0 to 4
pts(i) = new Point( items(i).ptX, items(i).ptY )
lines( i ) = new Point( items(i).x1, items(i).y1 );
Next

g.FillPolygon( Brushes.LightGreen, pts );
g.DrawLines( Pens.LightGray, lines );

Not sure I've followed your code correctly, the picture shows a connected
line, so I'm assuming items(i).x2, items(i).y2 matches items(i+1).x1 and
items(i+1).y here.

n!
Oct 26 '06 #4
Hi,

great, that's the best idea I've heard so far, so I think that's what
I'll use :) Altough I have to declare and use another array of points,
that would at least solve my problem of looping twice.

And well, my example here is really simplified, in my real context, I
loop in fact from 0 to (items.count-1), and draw lines from
items(i).x1, items(i).y1 to items(i+1).x1, items(i+1).y1, so that gives
a connected line (there is in fact no x2 and y2 per item)...didn't
thought to include this here :)

thank you very much again, help is much appreciated!

n! wrote:
So I need to build my pts array in order to be able to draw my polygon,
but while I loop for that, I thought 'why not draw the lines'...Now, I
end up with my polygon drawn after, because I can only draw it once I
have all the points. If I want to draw my lines after, do I really have
to add another loop? So one loop to build points, then draw polygon,
then loop again through the same data to draw lines?...I'd really like
to avoid looping twice, because let's say I have 20,000 items, that
double loop is starting to be really expensive...

You can also build a list of lines along with the polygon, modifying the
original code:

Dim pts( 5 ) As Point
Dim lines( 5 ) As Point
Dim i as integer
For i = 0 to 4
pts(i) = new Point( items(i).ptX, items(i).ptY )
lines( i ) = new Point( items(i).x1, items(i).y1 );
Next

g.FillPolygon( Brushes.LightGreen, pts );
g.DrawLines( Pens.LightGray, lines );

Not sure I've followed your code correctly, the picture shows a connected
line, so I'm assuming items(i).x2, items(i).y2 matches items(i+1).x1 and
items(i+1).y here.

n!
Oct 26 '06 #5
Hi,

great, that's the best idea I've heard so far, so I think that's what
I'll use :) Altough I have to declare and use another array of points,
that would at least solve my problem of looping twice.

And well, my example here is really simplified, in my real context, I
loop in fact from 0 to (items.count-2), and draw lines from
items(i).x1, items(i).y1 to items(i+1).x1, items(i+1).y1, so that gives
a connected line (there is in fact no x2 and y2 per item)...didn't
thought to include this here :)
but your idea of building an array of points to draw lines fits
perfectly my needs (I wasn't aware of the drawlineS method, which takes
an array of points)

thank you very much again, help is much appreciated!

n! wrote:
So I need to build my pts array in order to be able to draw my polygon,
but while I loop for that, I thought 'why not draw the lines'...Now, I
end up with my polygon drawn after, because I can only draw it once I
have all the points. If I want to draw my lines after, do I really have
to add another loop? So one loop to build points, then draw polygon,
then loop again through the same data to draw lines?...I'd really like
to avoid looping twice, because let's say I have 20,000 items, that
double loop is starting to be really expensive...

You can also build a list of lines along with the polygon, modifying the
original code:

Dim pts( 5 ) As Point
Dim lines( 5 ) As Point
Dim i as integer
For i = 0 to 4
pts(i) = new Point( items(i).ptX, items(i).ptY )
lines( i ) = new Point( items(i).x1, items(i).y1 );
Next

g.FillPolygon( Brushes.LightGreen, pts );
g.DrawLines( Pens.LightGray, lines );

Not sure I've followed your code correctly, the picture shows a connected
line, so I'm assuming items(i).x2, items(i).y2 matches items(i+1).x1 and
items(i+1).y here.

n!
Oct 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: **ham | last post by:
I know that's an old dirty issue; GDI+ almost -the slowest part of the framework - has bothered many developers using it in animations. Even in managed C++ the performance is awful. Now, any dude...
2
by: °Ë´óɽÈË | last post by:
Hi guys, As you know, we can call SetROP2 to set the current foreground mix mode in GDI. But I doesn't work in GDI+. The step is: GetDC --> SetROP2 -->Graphics.FromDC, then draw sth by graphics...
6
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...
1
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...
6
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...
0
by: Brian Keating | last post by:
hi there i've a test program that creates a treeview and destroys it over and over, i keep track of the gdi object count for the process and see if they are ok. However when i switch on...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
7
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"...
5
by: Jonathan Boivin | last post by:
Hi, I've got some problems with loading bills using a bill usercontrol I built. If I load all current bills in my test environment (156) everything is fine once, but repeating the operation...
1
by: Chris Dunaway | last post by:
When working with GDI+, calling the CreateGraphics method to draw on a control has normally been frowned upon and it is always emphasized to dispose of pens and brushes and other GDI objects lest...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.