How to draw a circle hole inside a rectangle in xaml - wpf

I need to put a image as background of whole page, and put over it a rectangle with a transparent circle hole. The example result is bellow.
Update
I found some solutions using Geometry.Combine, but this does not exists in WP8. With this aproach I could draw a rectangle and a circle and combine both using GeometryCombineMode.Exclude. But this method seems to not exist in WP8. How create something that can I achieve a similar result to Geometry.Combine to create a hole inside a element?

You should look into the Path element and learn the mini-language for the path data. This is a rough example:
<Path x:Name="path" Data="M0,100 v-50 h100 a10,10,0,1,0,50,0 h100 v50 z" Fill="Gray" />
Basically:
Move down 100 px
vertical line up 50 px
horizontal line 100 px
arc of radius 10 px (with some magic; read the docs ;-) )
horizontal line of 100 px
vertical line of 50 px
**z* (automatically complete the path)

Related

Increase StrokeThickness but maintain dimensions of Path

I have a Path that normally has a StrokeThickness of 1. Under certain circumstances, it needs to have a StrokeThickness of 10. When I increase the stroke thickness, I don't want the path to take any additional space.
By default, just increasing the StrokeThickness increases the rendered size of the path. So you get something like this (the blue outline extends beyond the black boundary):
This is what I'm trying to achieve (the blue outline stays within the black boundary):
I can think of two mathematical ways to compensate for the increased StrokeWidth:
Manually adjust the points of the triangle inward.
Use a ScaleTransform on the Geometry of the Path.
Both of these would be somewhat problematic/complex. Is there an easier way?
You could clip the path by its own geometry like this:
<Path ... Clip="{Binding Data, RelativeSource={RelativeSource Self}}"/>
but then you would need to double the StrokeThickness, since only half of the stroke is visible.
On a whim I set StrokeThickness = -1 on my Rectangle and it did exactly what I wanted it to: the stroke goes on the inside of the Rectangle rather than on the outside.

Filling not closed PathGeometry to the bottom

Suppose I have PathGeometry, consisting of lines, like this
(the rectangle == panel, for example Grid):
I want to fill them to the bottom of the panel, like this:
The quick and not very good solution I see is to create additional curve with 2 additional points in the bottom and use it for filling.
Is there some better way to solve the task?
Something like this (pseudocode):
<Path Data=... FillStyle = "ToTheBottom" Fill="Blue"/>
There is no standard way of doing this; there is no Fill like this defined in WPF.
You could put two path geometries on top of each other. The bottom one would have a stroke thickness of 0 and has 2 extra points (those on the lower edge of the rectangle)
The second one the top would simply be the geometry you have now.
If you need to draw a lot of these you might create a custom control that does this for you.

How can I draw a circle sector with the ellipse class?

I would like to make a sector of a circle on WP7. I tried to do this with the ellipse class and I found a lot of solution, which make a gauge or pie chart or something, but I need just the essence.
Could anyone help?
the aim is to show just one part of a circle (or ellipse). Like the yellow area in the picture:
Thanks,
Laci
Here's a fairly simple solution to the problem, though it does not use an Ellipse and it requires a little trigonometry:
<Path Fill="Black"
Data="M0,0 L0,-100 A100,100 0 0 1 70.7,-70.7 z" />
The Data property uses Path Markup Syntax.
The "M" at the beginning tells the pen to Move to the location 0,0.
The "L" tells the pen to draw a Line from the current location (0, 0) to 0,-100.
The "A" tells the pen to draw an elliptical Arc from the current location to 70.7,-70.7 (the "100,100" portion determines the horizontal and vertical radius of the ellipse and the "0 0 1" portion is for RotationAngle, IsLargeArc, and SweepDirection (1 for clockwise, 0 for counter-clockwise)).
The "z" tells the pen to close or complete the shape (which will cause a line to be drawn from 70.7,-70.7 back to 0,0).
Where did the 70.7 come from? Well, this particular arc sweeps out an angle of 45 degrees from a circle with radius 100, so the coordinates 70.7,-70.7 are determined by 100 * sin(45) and 100 * cos(45).
You need to do something like this:
define a canvas wrapper for ellipse
define the visible part of the canvas (clip). For this part you need to use PathGeometry as the Clip to define the slice of the circle you want to be visible. (see link)
<Canvas>
<Canvas.Clip>
<PathGeometry>
// define your path here (see link above)
</PathGeometry>
<Ellipse Background="Yellow" Width="200" Height="200" />
</Canvas.Clip>
</Canvas>
Alternatively you can use CombinedGeometry to combine a PathGeometry and EllipseGeometry to form the slice. (the link provides a good example of CombinedGeometry)

How to make some layer mask in Silverlight?

I have an image object and a rectangle object. Now I want to be the image to be only visible where the rectangle is, everything else should have an opacity of 0.5 - the result should look something simliar to the following:
When I set the opacity of the rectangle to 0.5 the effect is the antipode - so how could I realize it as shown in the image? Size and position of the rectangle is changed by code-behind, but that shouldn't make any differences...
All hints / answeres appreciated :)
If you want to add an opaque mask to partially hide your image outside of a rectangle, it's rather easy.
<Grid>
<Image Source="myImage.jpg" Opacity="0.5/>
<Image Source="myImage.jpg" >
<Image.Clip>
<RectangleGeometry Rect="x,y,w,h"/>
</Image.Clip>
</Image>
</Grid>
Where x,y,w and h are your rectangle position and size (see MSDN). If you want to move the visible portion around, set the Clip property by code.
If you want to add the "black stroke effect", you could simply add a Rectangle with position and size matching those of your clipping path after the second image in your grid.
Do you want more infos?

WPF: How do I create a background that repeats horizontally without scaling?

I would like to create a background for my window which is an image that I want repeated horizontally. So far I've tried with the ImageBrush, but this option repeats the image horizontally and vertically. Also, I don't want it to scale when user resize the window, as it makes the image look funny.
If what you want to do is tile an image horizontally as you would in CSS with the simple one liner "background-repeat: repeat-x" then after some (!) trial and error what you need in XAML is this:
<ImageBrush ImageSource="Images/my-background-image.png"
TileMode="FlipY"
Stretch="Uniform"
AlignmentY="Top"
Viewport="0,0,90,3000"
ViewportUnits="Absolute" />
Where the last 2 values on the Viewport attribute are the width of your image in pixels and then a very large number that is higher than your viewport height so that the image is not repeated in the Y direction within that height.

Resources