WPF FAQ - 2D Graphics

Find answers for the most frequently asked questions
Expand All Collapse All

CombinedGeometry is used to combine two geometries. By updating the GeometryCombineMode property, along with Union, Intersect, Exclude and Xor options, we can achieve CombinedGeometry. This can be done with the following code snippets.

[XAML]
<Path Stroke="Black" StrokeThickness="1" Fill="Blue">
    <Path.Data>
        <CombinedGeometry GeometryCombineMode="Xor">
            <CombinedGeometry.Geometry1>
                <EllipseGeometry RadiusX="70" RadiusY="40" Center="50,50" />
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <EllipseGeometry RadiusX="50" RadiusY="30" Center="50,50" />
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>

Reference link: https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-create-a-combined-geometry

Permalink

BitmapEffect can be applied to a particular area of an Image using the ‘BitmapEffectInput’ property of the ‘Image’ class. ‘AreaToApplyEffect’ property of the BitmapEffectInput markup extension is used to specify the area and ‘AreaToApplyEffectUnits’ property is used to specify the units.

The following lines of code are used to apply BitmapEffect to a lower part of the image.

[XAML]

<Image Source='pda.ico' Height='160' Width='160'>
<Image.BitmapEffect>
      	<BevelBitmapEffect BevelWidth='10' EdgeProfile='BulgedUp'/>
</Image.BitmapEffect>
<Image.BitmapEffectInput>
<BitmapEffectInput AreaToApplyEffect='0,0.5,1,0.5' AreaToApplyEffectUnits='RelativeToBoundingBox'/>
</Image.BitmapEffectInput>
</Image>
Permalink

The visual tree determines the rendering order of WPF visual and drawing objects. The rendering order of traversal starts with the root visual, which is the top most element in the root visual tree. The root visual’s children are then traversed, left to right. Find the following button control rendering order in visual tree.

Reference link: https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/wpf-graphics-rendering-overview

Permalink

Any type of transform can be applied when an event has occurred.

The following lines of code apply a ’RotateTransform’ when the mouse is moved over the image.

[XAML]

<Image Source='pda.ico' MouseEnter='Image_MouseEnter' MouseLeave='Image_MouseLeave'>
<Image.RenderTransform>
<RotateTransform Angle='0' x:Name='ImageRot'/>
</Image.RenderTransform>
</Image>
[C#]

private void Image_MouseEnter(object sender, MouseEventArgs e)
{
ImageRot.Angle = 30;
}
private void Image_MouseLeave(object sender, MouseEventArgs e)
{
ImageRot.Angle = 0;
}

Permalink

A WIN32 application uses immediate mode graphics system wherein the application is responsible for repainting the client area when they are resized or the object’s visual appearance is changed. WPF on the contrary uses retained mode graphics system. In this mode, drawing information of an object is persisted and serialized by the application and the rendering is done by the system.

Permalink

WPF has a “VisualTreeHelper” class that provides the functionality to retrieve the parent objects of a visual object using the GetParent() method and child objects of a visual object using the GetChild() method by specifying the index value of the child.

The following code snippet is used to enumerate all the child objects of a visual object.

[C#]

public void ChildEnum(Visual Visualobj)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(Visualobj); i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(Visualobj, i);
VisualEnum(childVisual);
}
}
Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.