I would like to know how can I draw an arc in C# , I'm trying using DrawEllipse but it doesn't work and it give wrong drawing.
However, I have searched for a method to draw an arc in the Class DrawingContext but I didn't find it.
DrawingVisual d = new DrawingVisual();
System.Windows.Media.Pen pen = new System.Windows.Media.Pen();
DrawingContext drawingContext = d.RenderOpen();
pen.Brush = System.Windows.Media.Brushes.Black;
System.Windows.Point center = new System.Windows.Point();
center.X = 0.4;
center.Y = 0.5;
drawingContext.DrawEllipse(System.Windows.Media.Brushes.White, pen, center, 4,4);
drawingContext.Close();
canvas.Children.Add(new VisualHost { Visual = d });
You would have to draw a PathGeometry or a StreamGeometry that contains an arc segment, like the following circular arc of radius 100 from (100,100) to (200,200):
var visual = new DrawingVisual();
var pen = new Pen(Brushes.Black, 1);
using (var dc = visual.RenderOpen())
{
var figure = new PathFigure
{
StartPoint = new Point(100, 100) // start point
};
figure.Segments.Add(new ArcSegment
{
Point = new Point(200, 200), // end point
Size = new Size(100, 100), // radii
SweepDirection = SweepDirection.Clockwise
});
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
dc.DrawGeometry(null, pen, geometry);
}
Related
This is my rectangle
protected void DrawRectangle(DrawingContext dc, Point point)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawContext = drawingVisual.RenderOpen())
{
Pen drawingPen = new Pen(ErrorBarBrush, ErrorBarThickness);
dc.DrawRectangle(Brushes.Red,
new Pen(Brushes.Black, 5),
new Rect(new Point(point.X - 50, point.Y + 50),
new Point(point.X + 50, point.Y - 50)));
dc.PushOpacity(2);
}
}
So my question is how do i set my opacity, is this right way to do it?
(This is changing the opacity of the Rectangle)
Instead of passing Brushes.Red into the Rectangle make a new SolidColorBrush and set the opacity of the SolidColorBrush you pass into the Rectangle
SolidColorBrush rectBrush = new SolidColorBrush(Colors.Red);
rectBrush.Opacity = 0.5; // or whatever
dc.DrawRectangle(rectBrush, ...
You'll need to do a similar thing for the Pen
Simply
drawingVisual.Opacity = 0.5;
I want to draw the Shape as in the image. I have drawn half circle using Arc Segment, Now I want to draw Quarter circle, or A Sector, But I am unable to draw it.
I used this code to draw Arc, I tried to change the size, and the angle is also not working.
What should I do to draw Quarter circle / Sector ?? Code I used to draw Arc is :
PathFigure pthFigure1 = new PathFigure();
pthFigure1.StartPoint = new Point(50, 60);// starting cordinates of arcs
ArcSegment arcSeg1 = new ArcSegment();
arcSeg1.Point = new Point(100, 82); // ending cordinates of arcs
arcSeg1.Size = new Size(10, 10);
arcSeg1.IsLargeArc = false;
arcSeg1.SweepDirection = SweepDirection.Clockwise;
arcSeg1.RotationAngle = 90;
PathSegmentCollection myPathSegmentCollection1 = new PathSegmentCollection();
myPathSegmentCollection1.Add(arcSeg1);
pthFigure1.Segments = myPathSegmentCollection1;
PathFigureCollection pthFigureCollection1 = new PathFigureCollection();
pthFigureCollection1.Add(pthFigure1);
PathGeometry pthGeometry1 = new PathGeometry();
pthGeometry1.Figures = pthFigureCollection1;
System.Windows.Shapes.Path arcPath1 = new System.Windows.Shapes.Path();
arcPath1.Data = pthGeometry1;
arcPath1.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 23, 0));
this.LayoutRoot.Children.Add(arcPath1);
With minimum changes to your code (/**/ signifies changed or added lines).
PathFigure pthFigure1 = new PathFigure();
/**/ pthFigure1.StartPoint = new Point(0, 100);// starting cordinates of arcs
ArcSegment arcSeg1 = new ArcSegment();
/**/ arcSeg1.Point = new Point(100, 0); // ending cordinates of arcs
/**/ arcSeg1.Size = new Size(100, 100);
arcSeg1.IsLargeArc = false;
arcSeg1.SweepDirection = SweepDirection.Clockwise;
/**/ LineSegment lineSeg = new LineSegment();
/**/ lineSeg.Point = new Point(100,100);
PathSegmentCollection myPathSegmentCollection1 = new PathSegmentCollection();
myPathSegmentCollection1.Add(arcSeg1);
/**/ myPathSegmentCollection1.Add(lineSeg);
pthFigure1.Segments = myPathSegmentCollection1;
PathFigureCollection pthFigureCollection1 = new PathFigureCollection();
pthFigureCollection1.Add(pthFigure1);
...
It seems that you missinterpret the meaning of ArcSegment.RotationAngle.
The math of it:
I would like to dynamically add a Triangle to my canvas. I can do this but only in one very specific way that does not work for my application. What I would like to do is supply a point and a size and get back a triangle.
var poly = shape as Polygon;
Polygon p = new Polygon
{
//Width = size,
//Height = size,
Fill = new SolidColorBrush(Colors.Red),
ManipulationMode = ManipulationModes.All,
RenderTransform = new CompositeTransform()
};
int w = 200;
int h = 200;
Point start = new Point(400, 200);
var right = new Point(start.X + w, start.Y);
var top = new Point(start.X + (w / 2), start.Y - (h));
poly.Points.Add(point);
poly.Points.Add(right);
poly.Points.Add(top);
poly.Points.Add(point);
Then I add the shape to my canvas child controls and and set the X Y on the shapes RenderTransform. Nothing appears. If however I just do this:
Polygon p = new Polygon
{
//Width = size,
//Height = size,
Fill = new SolidColorBrush(Colors.Red),
ManipulationMode = ManipulationModes.All,
RenderTransform = new CompositeTransform()
};
p.Points.Add(new Point(300, 200));
p.Points.Add(new Point(400, 125));
p.Points.Add(new Point(400, 275));
p.Points.Add(new Point(300, 200));
It renders a Triangle just fine. However if you supply a width and height to the above code it will stop rendering.
Can I just create a triangle of a certain size, without having to set actual points on the canvas at first, much like you do an Ellipse or a Rectangle?
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;
I have created a c# windows application and written 75% of the code. The program allows the user to create a flow chart, and will shade the flow chart shapes according to their status. I wanted them to become 3d buttons such as from the website Webdesign.org
Rather than create a PNG for each button, I wanted to create them in C# using brushes or other technique, such as:
// Create solid brush.
SolidBrush blueBrush = new SolidBrush(Color.Blue);
// Create points that define polygon.
PointF point1 = new PointF(50.0F, 50.0F);
PointF point2 = new PointF(100.0F, 25.0F);
PointF point3 = new PointF(200.0F, 5.0F);
PointF point4 = new PointF(250.0F, 50.0F);
PointF point5 = new PointF(300.0F, 100.0F);
PointF point6 = new PointF(350.0F, 200.0F);
PointF point7 = new PointF(250.0F, 250.0F);
PointF[] curvePoints = {point1, point2, point3, point4, point5, point6, point7};
// Define fill mode.
FillMode newFillMode = FillMode.Winding;
// Fill polygon to screen.
e.Graphics.FillPolygon(blueBrush, curvePoints, newFillMode);
I know WPF has radial gradients, but can I do something simular in CGI?
Unlike WPF, GDI+/WinForms does not have a RadialGradientBrush. However you can achieve the same effect using a PathGradientBrush.
Here's an example:
Rectangle bounds = ...;
using (var ellipsePath = new GraphicsPath())
{
ellipsePath.AddEllipse(bounds);
using (var brush = new PathGradientBrush(ellipsePath))
{
brush.CenterPoint = new PointF(bounds.Width/2f, bounds.Height/2f);
brush.CenterColor = Color.White;
brush.SurroundColors = new[] { Color.Red };
brush.FocusScales = new PointF(0, 0);
e.Graphics.FillRectangle(brush, bounds);
}
}
The PathGradientBrush has lots of properties to experiment with to ensure you get the effect you're after.
Take a look at this Codeproject article, and then at the Path gradient.