Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Situation: White rectangle is drawn on DrawingVisual.
Goal: Clear a part (geometry) of it to make it transparent.
Windows Forms alternative: Graphics.Clear.
Code:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        visuals = new VisualCollection(this);
        DrawIt();
    }

    VisualCollection visuals;

    void DrawIt()
    {
        DrawingVisual visual = new DrawingVisual();
        using (DrawingContext dc = visual.RenderOpen())
        {
            dc.DrawRectangle(
                Brushes.White, new Pen(),
                new Rect(10, 10, 100, 100));

            dc.DrawRectangle(
                Brushes.Red, new Pen(),
                new Rect(50, 50, 20, 20)); // Remove it
        }
        visuals.Add(visual);
    }

    protected override Visual GetVisualChild(int index)
    {
        return visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get { return visuals.Count; }
    }
}
Posted
Updated 3-Aug-15 5:30am
v2
Comments
Sergey Alexandrovich Kryukov 14-Jul-15 15:42pm    
Why would you ever need something like clear? This is just a wrong conception. Just don't draw what you don't want to render. I'm not joking; this is the right approach.
—SA
Ziya1995 15-Jul-15 3:31am    
Because of the next goal:
http://www.codeproject.com/Questions/1009148/How-to-make-a-glow-effect-for-a-line-shape
If you know how to solve the problem in a better way, i would be happy to know.

While i am reading your solution.
Sergey Alexandrovich Kryukov 15-Jul-15 7:01am    
This is a complicated problem and... ill-posed. There is no such technical thing as "glow". You are going into the field of arts, how to create impression of glowing. If you could described it how it should technically look, we could discuss the implementation, but it also would be not easy. May I ask you: why?
—SA
Ziya1995 15-Jul-15 7:41am    
My link has a code which does it.
I also can create it very easy in Windows Forms, because i can clear.
You need to read what i wrote in another thread i linked to to understand.

The principle is simple using StrokeThickness.

While i have not analyzed your first solution, i feel like it has nothing related to my problem.

If you wanna understand, you can check the code in the other thread to see what happens.

> There is no such technical thing as "glow".
Wrong, it is absolutely the certain thing, it is not about some "super" effects, it is about the exact thing i can do in Windows Forms.
If you will read the other thread, you can see WPF used to support it as one function.
Sergey Alexandrovich Kryukov 15-Jul-15 7:56am    
All right, it the context of your question it does exist, in that sense. Thank you for the explanation.
—SA

This can be rendered in different ways. I would suggest the following: define some Geometry of non-rectangular shape and assign the instance to the property System.Windows.Media.Visual.VisualClip:
https://msdn.microsoft.com/en-us/library/system.windows.media.visual.visualclip%28v=vs.110%29.aspx[^].

Say, if you want a transparent round hole in your Visual, create an instance of Geometry with the hole in it:
https://msdn.microsoft.com/en-us/library/system.windows.media.geometry%28v=vs.110%29.aspx[^].

You can use derived classes like System.Windows.Media.PathGeometry or System.Windows.Media.StreamGeometry to defined geometry based on some arbitrary path or StreamGeometryContext:
https://msdn.microsoft.com/en-us/library/system.windows.media.pathgeometry%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.media.streamgeometrycontext%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v3
1. Create 2 geometries - a drawing area and an area to clear.
2. Use PushClip of DrawingContext to clip the combined geometry of the first 2 ones combined by Exclude mode.
Warning: Use GetOutlinedPathGeometry()! Operations may be done without it too, but it will get more artifacts with complex geometries, if you don't use it. Artifacts will appear by anyway, but much more less than without GetOutlinedPathGeometry().

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        visuals = new VisualCollection(this);
        DrawIt();
    }

    VisualCollection visuals;

    void DrawIt()
    {
        DrawingVisual visual = new DrawingVisual();
        using (DrawingContext dc = visual.RenderOpen())
        {
            RectangleGeometry rect1 = new RectangleGeometry();
            rect1.Rect = new Rect(10, 10, 100, 100);

            RectangleGeometry rect2 = new RectangleGeometry();
            rect2.Rect = new Rect(50, 50, 20, 20); // Removed it

            Geometry geom1 = rect1.GetOutlinedPathGeometry();
            Geometry geom2 = rect2.GetOutlinedPathGeometry();

            //Geometry combined1 = new CombinedGeometry(GeometryCombineMode.Exclude, rect1, rect2); // Not recommended
            Geometry combined2 = new CombinedGeometry(GeometryCombineMode.Exclude, geom1, geom2);

            dc.PushClip(combined2);

            dc.DrawRectangle(
                Brushes.White, new Pen(),
                new Rect(10, 10, 100, 100));
        }
        visuals.Add(visual);
    }

    protected override Visual GetVisualChild(int index)
    {
        return visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get { return visuals.Count; }
    }
}
 
Share this answer
 
v6
Comments
Philippe Mori 3-Aug-15 12:39pm    
There is no explanation with the code!
Ziya1995 6-Aug-15 13:18pm    
I added the explanation.

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