One circle and line inside it with radius 56 - c#

How to draw line inside circle, line will be visible only inside circle and I need to have control on line angel. Let say one circle and line inside it with angel 56. How to do this in C# WPF.

Just put an Ellipse and a Line in a Panel that lets them draw on top of each other (such as a Grid), and adjust the parameters to whatever you want.
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.LayoutTransform>
<RotateTransform Angle="56" CenterX="28" CenterY="28"/>
</Grid.LayoutTransform>
<Ellipse Height="56" Width="56" Stroke="Red" StrokeThickness="2" />
<Line X1="1" X2="55" Y1="28" Y2="28" Stroke="Red" StrokeThickness="2" />
</Grid>

If you want to do it in XAML, do it like this:
<Line X1="1" X2="55" Y1="28" Y2="28" Stroke="Red" StrokeThickness="2" >
<Line.LayoutTransform>
<RotateTransform Angle="56" CenterX="50" CenterY="50"/>
</Line.LayoutTransform>
</Line>

If you read through the respective overviews you should be able to manage: Geometry & Shapes

Related

How to add to Ellipse a polygon.

Is it possible to draw polygon on ellipse in xaml?
Something like this:
<Ellipse
<Polygon Points="8,0 16,16, 0,16" Fill="Black" Margin="143,51,141,233" />
I need it because I would like to rotate an elipse. Polygon should rotate with elipse.
Put both shapes in a canvas and rotate the canvas using RotateTransform
<Canvas>
<Canvas.RenderTransform>
<RotateTransform CenterX="0" CenterY="0" Angle="45" />
</Canvas.RenderTransform>
<Ellipse />
<Polygon />
</Canvas>

How to make a custom shape with properties in WPF?

How to create a custom shape in WPF? For example, I am trying to draw multiple symbols connected between each other on a cavas. I would need each symbol to change the color as well. How would I create let's say a custom shape that can change colors. Let's say the following two shapes I would like to make into one. how would I go to encapsulate them
<Ellipse x:Name="theEllipse"
Height="89"
Width="82"
Fill="Black"
Canvas.Left="32"
Canvas.Top="75" />
<Ellipse x:Name="theEllipse"
Height="89"
Width="82"
Fill="Black"
Canvas.Left="32"
Canvas.Top="75" />
The solution provided below does work with the ellipses however,
it does not work with the following figures:
<Line X1="10"
Y1="50"
X2="50"
Y2="50"
Stroke="Black"
StrokeThickness="2" />
<Line X1="15"
Y1="55"
X2="45"
Y2="55"
Stroke="Black"
StrokeThickness="2" />
<Line X1="20"
Y1="60"
X2="40"
Y2="60"
Stroke="Black"
StrokeThickness="2" />
You can do it in Blend.
Select the two ellipses/shapes in the Objects and Timeline window
Right-click on the selection
Go to Path -> Make compound path
Update 2017-04-10
If you want this operation to work with all shapes and lines, you have to make sure your elements are inside a Canvas, not a Grid or StackPanel. The parent container of the elements matters.
So in the case of the 3 lines, wrap them inside a Canvas and run the Make Compound Path operation again.
<Canvas>
<Line X1="10"
Y1="50"
X2="50"
Y2="50"
Stroke="Black"
StrokeThickness="2" />
<Line X1="15"
Y1="55"
X2="45"
Y2="55"
Stroke="Black"
StrokeThickness="2" />
<Line X1="20"
Y1="60"
X2="40"
Y2="60"
Stroke="Black"
StrokeThickness="2" />
</Canvas>
This is what it should look like after:
<Canvas>
<Path Data="M1,1 L41,1 M6.00001,6 L36,6 M11,11 L31,11"
Height="12"
Canvas.Left="9"
Stretch="Fill"
Stroke="Black"
StrokeThickness="2"
Canvas.Top="49"
UseLayoutRounding="False"
Width="42" />
</Canvas>

Change absolute positioning in Canvas from code

I am creating my own usercontrol where I have a Canvas which will hold Hexagons.
I'm now at the stage where I'm adding the function AddItem, and since I don't want the hexagons to overlap I obviously want to use the Canvas' positioning methods. But how do I access them from "normal" code?
I have a Path object hexagon which holds the hexagon-data and I want to add it to my canvas aka container
So how do I change the positioning from c# code?
I know that in XAML my path looks like this when inside the Canvas (please notice Canvas.Left and Canvas.Top) , you'll have to scroll a bit..
<Canvas x:Name="Container" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<Path d:LayoutOverrides="None" d:LastTangent="0,0" Stroke="White" Fill="#343434" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" Width="50" Height="50" Stretch="Fill" Data="M8.660254,0 L17.320508,5 17.320508,15 8.660254,20 0,15 0,5 8.660254,0 z"/>
<Path d:LayoutOverrides="None" d:LastTangent="0,0" Stroke="White" Fill="#343434" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" Width="50" Height="50" Stretch="Fill" Data="M8.660254,0 L17.320508,5 17.320508,15 8.660254,20 0,15 0,5 8.660254,0 z" Canvas.Left="25" Canvas.Top="37.5"/>
</Canvas>
Use this method
Path myPath = ....; // obtain your path here
Canvas.SetLeft(myPath,25);
Canvas.SetTop(myPath,25);

Shape with slanted lines as background

Is possible to draw a Shape that has a background made by slanted lines?
An example with Rectangle (sorry for the image quality):
And if i want dashed lines or change the line properties (stroke, thickness..)?
You can use solution from this article http://mark-dot-net.blogspot.com/2007/06/creating-hatched-patterned-brush-in-wpf.html
<VisualBrush
x:Key="HatchBrush"
TileMode="Tile" Viewport="0,0,10,10"
ViewportUnits="Absolute" Viewbox="0,0,10,10"
ViewboxUnits="Absolute">
<VisualBrush.Visual>
<Canvas>
<Rectangle Fill="Azure" Width="10" Height="10" />
<Path Stroke="Purple" Data="M 0 0 l 10 10" />
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
Just change the parameters etc. to fit your application
Usage:
<Rectangle Width="80" Height="40"
Fill="{StaticResource HatchBrush}"/>
Does this help: [WPF, C#: Draw a line onto existing bitmap in image control] or http://msdn.microsoft.com/en-us/library/ms747393.aspx

Rendering sharp lines in WPF

If I render the following:
<Grid>
<Canvas SnapsToDevicePixels="True">
<Path Fill="#FF000000" SnapsToDevicePixels="True" Data="M 0.00,0.00 L 2.50,0.00 0.00,10.00 " />
<Path Fill="#FF260014" SnapsToDevicePixels="True" Data="M 2.50,0.00 L 7.50,0.00 2.50,10.00 0.00,10.00 " />
<Canvas.RenderTransform>
<ScaleTransform ScaleX="{Binding ElementName=slider,Path=Value}" ScaleY="{Binding ElementName=slider,Path=Value}" />
</Canvas.RenderTransform>
</Canvas>
<Slider x:Name="slider" Minimum="0" Maximum="50" Value="30"/>
</Grid>
I get this result (Kaxaml):
Notice the thin white line between the two shapes. I searched around and found out this has to do with pixel alignment. I would expect that settings SnapsToDevicePixels="True" would be enough to get rid of the line, but this doesn't work!
Any ideas how to get rid of the white line?
Try turning edge aliasing on with RenderOptions, like this (see Grid properties)
<Grid RenderOptions.EdgeMode="Aliased">
<Canvas SnapsToDevicePixels="True">
<Path Fill="#FF000000" SnapsToDevicePixels="True" Data="M 0.00,0.00 L 2.50,0.00 0.00,10.00 " />
<Path Fill="#FF260014" SnapsToDevicePixels="True" Data="M 2.50,0.00 L 7.50,0.00 2.50,10.00 0.00,10.00 " />
<Canvas.RenderTransform>
<ScaleTransform ScaleX="{Binding ElementName=slider,Path=Value}" ScaleY="{Binding ElementName=slider,Path=Value}" />
</Canvas.RenderTransform>
</Canvas>
<Slider x:Name="slider" Minimum="0" Maximum="50" Value="30"/>
</Grid>
Remember that SnapsToDevicePixels only controls that individual points do not lie on fractional pixel values. For horizontal and vertical lines this is most easily observed. In your case you are seeing an entirely different problem. The edges of your shapes are anti-aliased and therefore blended with the background. Since your shapes are exactly adjacent to each other both will be blended with the white background of the window. You can try putting one shape behind the other instead:
<Canvas>
<Path Fill="#FF000000" Data="M 0.00,0.00 L 7.50,0.00 2.50,10.00 0.00,10.00 " />
<Path Fill="#FF260014" Data="M 2.50,0.00 L 7.50,0.00 2.50,10.00 0.00,10.00 " />
<Canvas.RenderTransform>
<ScaleTransform ScaleX="{Binding ElementName=slider,Path=Value}" ScaleY="{Binding ElementName=slider,Path=Value}" />
</Canvas.RenderTransform>
</Canvas>
which should render correctly. You see similar rendering errors in many vector file formats that render primarily to screen, such as SVG.
The other option would be to turn off anti-aliasing but that will make your edges jaggy which may not be what you want (anti-aliasing turned off in the upper half):

Categories

Resources