I have a c# class library that supports Silverlight4 as well.
The problem is , I need to use System.Drawing.Drawing2d.GraphicsPath in my code. for drawing a shape. but it is not there in silverlight. Can anyone suggest me an alternative?
Thanks in advance.
Regards,
James
take a look at http://www.c-sharpcorner.com/UploadFile/mahesh/PathInSL03252009005946AM/PathInSL.aspx
Snippet XAML:
<Path Stroke="Black" StrokeThickness="4"
Data="M 80,200 A 100,50 45 1 0 100,50" />
Snippet Dynamic:
public void CreateAPath()
{
// Create a blue and a black Brush
SolidColorBrush blueBrush = new SolidColorBrush();
blueBrush.Color = Colors.Blue;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
// Create a Path with black brush and blue fill
Path bluePath = new Path();
bluePath.Stroke = blackBrush;
bluePath.StrokeThickness = 3;
bluePath.Fill = blueBrush;
// Create a line geometry
LineGeometry blackLineGeometry = new LineGeometry();
blackLineGeometry.StartPoint = new Point(20, 200);
blackLineGeometry.EndPoint = new Point(300, 200);
// Create an ellipse geometry
EllipseGeometry blackEllipseGeometry = new EllipseGeometry();
blackEllipseGeometry.Center = new Point(80, 150);
blackEllipseGeometry.RadiusX = 50;
blackEllipseGeometry.RadiusY = 50;
// Create a rectangle geometry
RectangleGeometry blackRectGeometry = new RectangleGeometry();
Rect rct = new Rect();
rct.X = 80;
rct.Y = 167;
rct.Width = 150;
rct.Height = 30;
blackRectGeometry.Rect = rct;
// Add all the geometries to a GeometryGroup.
GeometryGroup blueGeometryGroup = new GeometryGroup();
blueGeometryGroup.Children.Add(blackLineGeometry);
blueGeometryGroup.Children.Add(blackEllipseGeometry);
blueGeometryGroup.Children.Add(blackRectGeometry);
// Set Path.Data
bluePath.Data = blueGeometryGroup;
LayoutRoot.Children.Add(bluePath);
}
Related
I'm trying to create a line with a 'barber shop pole' gradient effect in C# (WPF). The code below works, but when I move the line the gradient becomes distorted. Is there a way to move/rotate the line object and have the gradient's angle remain stationary relative to the line itself.
// Create a linear gradient brush
LinearGradientBrush redWhiteStripes = new LinearGradientBrush();
redWhiteStripes.StartPoint = new Point(0, 0);
redWhiteStripes.EndPoint = new Point(1, 1);
redWhiteStripes.SpreadMethod = GradientSpreadMethod.Reflect;
ScaleTransform s = new ScaleTransform();
s.ScaleX = 0.125;
s.ScaleY = 0.125;
RotateTransform rot = new RotateTransform();
rot.Angle = 20;
rot.CenterX = 0.0625;
rot.CenterY = 0.0625;
TransformGroup tgroup = new TransformGroup();
tgroup.Children.Add(s);
tgroup.Children.Add(rot);
redWhiteStripes.RelativeTransform = tgroup;
// Create and add Gradient stops
GradientStop point1 = new GradientStop();
point1.Color = Colors.DarkRed;
point1.Offset = 0.0;
redWhiteStripes.GradientStops.Add(point1);
// Create and add Gradient stops
GradientStop point2 = new GradientStop();
point2.Color = Colors.DarkRed;
point2.Offset = 0.5;
redWhiteStripes.GradientStops.Add(point2);
// Create and add Gradient stops
GradientStop point3 = new GradientStop();
point3.Color = Colors.White;
point3.Offset = 0.5;
redWhiteStripes.GradientStops.Add(point3);
// Create and add Gradient stops
GradientStop point4 = new GradientStop();
point4.Color = Colors.White;
point4.Offset = 1.0;
redWhiteStripes.GradientStops.Add(point4);
Line l = new Line();
l.Stroke = redWhiteStripes;
l.StrokeThickness = 8;
I have this XAML code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer x:Name="ScrollViewer" Grid.Row="0" Background="Red">
<StackPanel x:Name="chat" >
</StackPanel>
</ScrollViewer>
</Grid>
And I am adding TextBlocks into the StackPanel called "chat" with this code:
public void ponerMensaje(string mensaje, bool me)
{
StackPanel panelTexto = new StackPanel();
panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal;
Thickness marginpanel = panelTexto.Margin;
marginpanel.Bottom = 10;
panelTexto.Margin = marginpanel;
//Create the colorBrush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Yellow;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
//Create the triangle
Polygon yellowTriangle = new Polygon();
yellowTriangle.Fill = yellowBrush;
//Create the triangle's points
System.Windows.Point Point1 = new System.Windows.Point(0, 0);
System.Windows.Point Point2 = new System.Windows.Point(10, 0);
System.Windows.Point Point3;
if (!me)
Point3 = new System.Windows.Point(10, 10);
else
Point3 = new System.Windows.Point(0, 10);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
//Add the points
yellowTriangle.Points = polygonPoints;
//Create the textblock
Grid gridParaTexto = new Grid();// In WP TextBlocks haven't Backgroundcolor
gridParaTexto.Background = yellowBrush;
TextBlock texto = new TextBlock();
texto.TextWrapping = TextWrapping.Wrap;
texto.Text = mensaje;
texto.Foreground = blackBrush;
gridParaTexto.Children.Add(texto);
//Add the message
if (!me)
{
panelTexto.Children.Add(yellowTriangle);
panelTexto.Children.Add(gridParaTexto);
chat.Children.Add(panelTexto);
}
else
{
panelTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
panelTexto.Children.Add(gridParaTexto);
panelTexto.Children.Add(yellowTriangle);
chat.Children.Add(panelTexto);
}
}
This code works, but the Textblock.TextWrapping not.
I'm new in WP maybe this code isn't the best, if you see other error, let me.
This line is the reason:
panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal;
When you set orientation of the StackPanel to horizontal, then it will be as wide as its content. You have to change your layout, for example use Grid instead.
I have modified your code. This should work as you described:
public void ponerMensaje(string mensaje, bool me)
{
Grid panelTexto = new Grid();
Thickness marginpanel = panelTexto.Margin;
marginpanel.Bottom = 10;
panelTexto.Margin = marginpanel;
//Create the colorBrush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Yellow;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
//Create the triangle
Polygon yellowTriangle = new Polygon();
yellowTriangle.Fill = yellowBrush;
//Create the triangle's points
System.Windows.Point Point1 = new System.Windows.Point(0, 0);
System.Windows.Point Point2 = new System.Windows.Point(10, 0);
System.Windows.Point Point3;
if (!me)
Point3 = new System.Windows.Point(10, 10);
else
Point3 = new System.Windows.Point(0, 10);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
//Add the points
yellowTriangle.Points = polygonPoints;
//Create the textblock
Grid gridParaTexto = new Grid();// In WP TextBlocks haven't Backgroundcolor
gridParaTexto.Background = yellowBrush;
TextBlock texto = new TextBlock();
texto.TextWrapping = TextWrapping.Wrap;
texto.Text = mensaje;
texto.Foreground = blackBrush;
gridParaTexto.Children.Add(texto);
panelTexto.Children.Add(yellowTriangle);
panelTexto.Children.Add(gridParaTexto);
//Add the message
if (!me)
{
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = GridLength.Auto,
});
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star),
});
Grid.SetColumn(gridParaTexto, 1);
Grid.SetColumn(yellowTriangle, 0);
chat.Children.Add(panelTexto);
}
else
{
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star),
});
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = GridLength.Auto,
});
gridParaTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
Grid.SetColumn(gridParaTexto, 0);
Grid.SetColumn(yellowTriangle, 1);
chat.Children.Add(panelTexto);
}
}
I have replaced StackPanel with Grid with two columns. One column is for yellow triangle and another one is for text.
I want to draw a signal image as shown below at runtime on canvas.
sample code which I used to draw this signal is as below.
Ellipse Ellipse1 = new Ellipse();
Ellipse Ellipse2 = new Ellipse();
Ellipse Ellipse3 = new Ellipse();
Line lineV = new Line();
Line lineH = new Line();
lineV.Stroke = SystemColors.WindowFrameBrush;
lineV.X1 = EndPosition.X;
lineV.Y1 = EndPosition.Y;
lineV.X2 = StartPosition.X;
lineV.Y2 = EndPosition.Y;
SolidColorBrush redBrush = new SolidColorBrush();
redBrush.Color = Colors.Black;
lineV.StrokeThickness = 2;
lineV.Stroke = redBrush;
canvas1.Children.Add(lineV);
lineH.Stroke = SystemColors.WindowFrameBrush;
lineH.X1 = StartPosition.X;
lineH.Y1 = EndPosition.Y;
lineH.X2 = StartPosition.X;
lineH.Y2 = StartPosition.Y;
redBrush.Color = Colors.Black;
lineH.StrokeThickness = 2;
lineH.Stroke = redBrush;
canvas1.Children.Add(lineH);
SolidColorBrush mySolidColorBrush1 = new SolidColorBrush();
mySolidColorBrush1.Color = Colors.Red; //FromArgb(255, 255, 255, 0);
Ellipse1.Fill = mySolidColorBrush1;
Ellipse1.StrokeThickness = 2;
Ellipse1.Stroke = Brushes.Black;
Ellipse1.Width = 30;
Ellipse1.Height = 30;
Ellipse1.Margin = new Thickness(EndPosition.X, EndPosition.Y - 15, EndPosition.X + 50, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse1);
SolidColorBrush mySolidColorBrush2 = new SolidColorBrush();
mySolidColorBrush2.Color = Colors.Green; //FromArgb(255, 255, 255, 0);
Ellipse2.Fill = mySolidColorBrush2;
Ellipse2.StrokeThickness = 2;
Ellipse2.Stroke = Brushes.Black;
Ellipse2.Width = 30;
Ellipse2.Height = 30;
Ellipse2.Margin = new Thickness(EndPosition.X + 30, EndPosition.Y - 15, EndPosition.X + 60, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse2);
SolidColorBrush mySolidColorBrush3 = new SolidColorBrush();
mySolidColorBrush3.Color = Colors.Yellow; // FromArgb(255, 255, 255, 0);
Ellipse3.Fill = mySolidColorBrush3;
Ellipse3.StrokeThickness = 2;
Ellipse3.Stroke = Brushes.Black;
Ellipse3.Width = 30;
Ellipse3.Height = 30;
Ellipse3.Margin = new Thickness(EndPosition.X + 60, EndPosition.Y - 15, EndPosition.X + 150, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse3);
**Now I want the user to be able to interactively move this signal on the canvas on mouse move events.
How can I able to do this ?**
Iam using C# WPF.
If want to implement a canvas to drag elements, a DragCanvas is your choice.
Dragging Elements in a Canvas from Josh Smith and after Dragging Elements in a Canvas from igkutikov that adapt Josh Smith code. There are articles with #mustread category.
With the dragCanvas you can implement a full functionality Dragging Element Canvas, and better adapt to your code. Happy coding!
I have two UIElements(i.e. rectangles) in Canvas and their coordinates. How can I connect them with arc in code behind?
No need to get an exact hit on the rectangles (or other objects): make sure the Z ordering is correct. arc.SetValue(Canvas.ZIndex, -1) will push it to the background. If you want a perpendicular hit, you'll need to break out the algebra :/
For the arc: (see http://msdn.microsoft.com/en-us/library/ms751808.aspx), it needs to be contained in a PathFigure.
Edit: this shows two connected rectangles. The line simple runs between the two centers. The arc starts on one center (the pathFigure startpoint), first argument is the center of the second object.
r1 = new Rectangle();
r1.Margin = new Thickness(50, 50, 0, 0);
r1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
r1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
r1.Height = 50;
r1.Width= 50;
r1.Fill = new SolidColorBrush(Colors.Red);
r2 = new Rectangle();
r2.Width = 50;
r2.Height = 50;
r2.Fill = new SolidColorBrush(Colors.Blue);
r2.Margin = new Thickness(350, 450, 0, 0);
r2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
r2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
l = new Line();
l.X1 = 75;
l.Y1 = 75;
l.X2 = 375;
l.Y2 = 475;
l.Fill = new SolidColorBrush(Colors.Purple);
l.Stroke = new SolidColorBrush(Colors.Purple);
l.StrokeThickness = 2;
l.SetValue(Canvas.ZIndexProperty, -1);
PathGeometry myPathGeometry = new PathGeometry();
// Create a figure.
PathFigure pathFigure1 = new PathFigure();
pathFigure1.StartPoint = new Point(75, 75);
pathFigure1.Segments.Add(
new ArcSegment(
new Point(375, 475),
new Size(50, 50),
45,
true, /* IsLargeArc */
SweepDirection.Clockwise,
true /* IsStroked */ ));
myPathGeometry.Figures.Add(pathFigure1);
// Display the PathGeometry.
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
myPath.SetValue(Canvas.ZIndexProperty, -1);
LayoutRoot.Children.Add(r1);
LayoutRoot.Children.Add(r2);
LayoutRoot.Children.Add(l);
LayoutRoot.Children.Add(myPath);
Am using the below code to create polygon. i just want to fill this polygon surface with black dots, how i can do that, then i want to convert this polygon to bitmap or in memory stream, how to do this??
// Create a blue and a black Brush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Transparent;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
// Create a Polygon
Polygon yellowPolygon = new Polygon();
yellowPolygon.Stroke = blackBrush;
yellowPolygon.Fill = yellowBrush;
yellowPolygon.StrokeThickness = 4;
// Create a collection of points for a polygon
System.Windows.Point Point1 = new System.Windows.Point(50, 100);
System.Windows.Point Point2 = new System.Windows.Point(200, 100);
System.Windows.Point Point3 = new System.Windows.Point(200, 200);
System.Windows.Point Point4 = new System.Windows.Point(300, 30);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
polygonPoints.Add(Point4);
// Set Polygon.Points properties
yellowPolygon.Points = polygonPoints;
// Add Polygon to the page
mygrid.Children.Add(yellowPolygon);
Do the dots have to be positioned in a particular order or do you just want to have a dotted pattern in your polygon without specific order?
If you don't need a special order you could use a Brush, a DrawingBrush for instance. Check out this link: http://msdn.microsoft.com/en-us/library/aa970904.aspx
You can then set this Brush as the Fill-Property of your Polygon instead of the SolidColorBrush.
This is the DrawingBrush example from the msdn link, but modified to display dots:
// Create a DrawingBrush and use it to
// paint the rectangle.
DrawingBrush myBrush = new DrawingBrush();
GeometryDrawing backgroundSquare =
new GeometryDrawing(
Brushes.Yellow,
null,
new RectangleGeometry(new Rect(0, 0, 100, 100)));
GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new EllipseGeometry(new Rect(0, 0, 20, 20)));
SolidColorBrush checkerBrush = new SolidColorBrush(Colors.Black);
GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);
DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);
myBrush.Drawing = checkersDrawingGroup;
myBrush.Viewport = new Rect(0, 0, 0.05, 0.05);
myBrush.TileMode = TileMode.Tile;
yellowPolygon.Fill = myBrush;