GDI+ Z-order?

I

ibiza

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! :)
 
N

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!
 
I

ibiza

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

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!
 
I

ibiza

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!
 
I

ibiza

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top