Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB .NET GDI+
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:
  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
O.G.I.3.2K
Edited 15-Jan-13 11:03am
v2
Comments
Sergey Alexandrovich Kryukov at 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. at 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 at 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 at 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.

1 solution

Rate this: bad
good
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.:
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
  Permalink  
Comments
O.G.I. at 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 at 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. at 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 at 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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 271
1 OriginalGriff 253
2 nv3 50
3 VJ Reddy 45
4 Mehdi Gholam 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
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