Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi guys,
I'm working on a night scene for my GDI game and I face some serious problems with the 'Graphics.SetClip' function. I use SetClip to make transparent regions where the spot lights will move, unfortunately when I intersect the two 'clip' regions they didn't merge, instead they make something like 'And' representation of each other:
http://dustwallow.webs.com/clip.png[^]

I played around with the 'CombineMode' property, but it didn't fix the problem. Here's the code of my 'Clipping' method:
VB
Public Sub SetClipRegion(e As PaintEventArgs, intX As Integer, intY As Integer, intrW As Integer, intH As Integer)
      Dim clipPath As New GraphicsPath()
      clipPath.AddEllipse(intX, intY, intrW, intH)
      clipPath.AddEllipse(intX + 100, intY + 100, intrW, intH)
      ' Set clipping region to path.
      e.Graphics.SetClip(clipPath, CombineMode.Replace)
      ' Fill rectangle to demonstrate clipping region.
      e.Graphics.FillRectangle(New SolidBrush(Color.Black), intX, intY, intrW, intH)
  End Sub

Please help. :(
Posted
Updated 15-Jan-13 11:03am
v2
Comments
Sergey Alexandrovich Kryukov 15-Jan-13 16:56pm    
Not clear. Your picture doesn't show what you want. Why CombineMode.Replace? What's the ultimate goal? Forget about regions for a minute, what's the effect you need, exactly?
—SA
O.G.I. 15-Jan-13 17:41pm    
The goal is:

- A black layer to cover the playground field.

- Transparent regions(ellipses) moving on random positions to show what is underneath the black layer.

The picture actually shows exactly what the problem is:
- when two 'clip' regions intersect with each other they didnt merge - instead they make a third black ellipse between them.
Neither one of the 'CombineMode' options solves the problem:
- I tried them all(Complement, Exclude, Intersect, Replace, Union and Xor), they produce different results but not merge.
Why 'clip' regions:
- because I don't want to waste computational power drawing something that is inside the dark regions.
Sergey Alexandrovich Kryukov 15-Jan-13 18:03pm    
There is no "merge". There is disjunction, conjunction, etc... Give it a right name; and half of problem is solved.
Also, what's the problem with transparent shapes? They can be drawn without clipping? Why would you need clipping at all.
—SA

Brisingr Aerowing 15-Jan-13 18:05pm    
How about the ExcludeClip method? Does that give the wanted result? There is also a Flatten method on the GraphicsPath. Try those.

Please see my comment to the question. There is no "merge", there is disjunction, conjunction, etc.

Here is my advice. Go step by step in finer detail. For that, don't use combine mode, but perform explicit operations on region objects. Disjunction is done using the Union methods, conjunction — via Intersect, etc.:
http://msdn.microsoft.com/en-us/library/system.drawing.region.aspx[^].

Also, these operations will make clip redundant. Actually, I advice you to avoid modifying clip region. You can do all your rendering on both shapes and regions:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fillregion.aspx[^].

For transparency, use alpha channel of the fill/outline colors. As simple as that.

There are many other methods to define a shape and render it with color/transparency.

—SA
 
Share this answer
 
Comments
O.G.I. 15-Jan-13 19:24pm    
Brisingr, thank you for the suggestions, but I couldn't make it work even with the 'Exclude Clip'.

Ok... let's put the question this way:

You have a path:
Dim clipPath As New GraphicsPath()

You add two shapes to this path - ellipses:
clipPath.AddEllipse(intX, intY, intrW, intH)
clipPath.AddEllipse(intX + 100, intY + 100, intrW, intH)

Then you set the clipping region to the path:
e.Graphics.SetClip(clipPath, CombineMode.Replace)

And you get this:
http://dustwallow.webs.com/clip.png
two intersected ellipses with third black ellipse between

how can I perform the 'Set.Clip' operation so the end result to be like this:
http://dustwallow.webs.com/clip2.png

I'm using Set.Clip function, because I don't want to display the areas in the dark, I don't want this areas to be calculated - only the spot light areas(the ellipses in the pictures).

Sorry, but I can't explain the problem more simply than this, I hope you understand...
Sergey Alexandrovich Kryukov 15-Jan-13 19:50pm    
I already explained. This is disjunction, but you tried to do Replace. Why? Get the union of two elliptical regions, isn't that easy?
—SA
O.G.I. 16-Jan-13 5:44am    
Unfortunately when you set the 'CombineMode' to Union the whole screen becomes blank with no dark areas at all. Either I don't know how exactly to do that, either it is impossible. If you know how to achieve this effect:

http://dustwallow.webs.com/clip2.png


Just tell me, I know what the technical terminology is, but here this isn't the case, the case is very simple: either you can do the effect shown on the picture above with Set.Clip or you can't. If you can tell me how, if you can't, then thank you for your efforts to help me, I'll continue to search for another ways.
Here is a picture showing all results from the CombineMode options(also shows what I need):
http://dustwallow.webs.com/Goal.png
Sergey Alexandrovich Kryukov 16-Jan-13 13:29pm    
I don't understand you. Listen carefully: I don't use CombineMode, you do. I always get the picture I want, and advise you to do the same.
You keep doing wrong things and talk as if I advised them. If you don't want to listen to the advise, it cannot be useful for you.
I told you all the terminology, just read again. What do you think is missing?
—SA
I know this is late, but for persons struggling with same problem, you could try to set FillMode of path to FillMode.Winding like so:
C#
...
clipPath.FillMode = FillMode.Winding;
e.Graphics.SetClip(clipPath, CombineMode.Exclude);
 
Share this answer
 
I wanted a region which was the union of 2 elliptical areas,
slightly displaced from each other.
When I added both these ellipses to a GraphicsPath, then
the region constructed from this path excluded the intersection
of the 2 ellipses, which was no good for me.

My (clumsy) solution was to get path1 from the 1st ellipse,
and path2 from the 2nd ellipse.
Then I got region1 from path1, and region2 from path2.
Finally the required union was region1 after:

region1.Union(region2);

Now region1 does not exclude the intersection of the 2 ellipses.
 
Share this answer
 
Comments
OriginalGriff 14-Sep-21 5:56am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 8 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900