Click here to Skip to main content
13,506,329 members
Rate this:
Please Sign up or sign in to vote.
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:[^]

I played around with the 'CombineMode' property, but it didn't fix the problem. Here's the code of my 'Clipping' method:
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 15-Jan-13 10:52am
Updated 15-Jan-13 11:03am
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?
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.

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.:[^].

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:[^].

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.

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:
two intersected ellipses with third black ellipse between

how can I perform the 'Set.Clip' operation so the end result to be like this:

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?
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:

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):
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:
clipPath.FillMode = FillMode.Winding;
e.Graphics.SetClip(clipPath, CombineMode.Exclude);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 24 Sep 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100