How to add a rectangle to PathFigure WPF - c#

I was totally tired of adding a Rectangle to a path figure. Below was my tried code, but it does not results correctly as a Rectangle:
PathGeometry geom = new PathGeometry();
Geometry g = new RectangleGeometry(myrectangel);
geom.AddGeometry(g);
PathFigureCollection collection = geom.Figures;
pathfigure = collection[0];
Is there any other way?

You can combined geometries using a GeometryGroup. A GeometryGroup creates a composite geometry from one or more Geometry objects.
GeometryGroup gg = new GeometryGroup();
gg.Children.Add(new RectangleGeometry(new Rect(0, 0, 100, 100)));
gg.Children.Add(new RectangleGeometry(new Rect(100, 100, 100, 100)));
gg.Children.Add(new RectangleGeometry(new Rect(200, 200, 100, 100)));
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
path.Data = gg;
path.Fill = Brushes.Blue;

Related

How can I draw an arc in C# WPF with parameters?

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);
}

Using RenderedGeometry as Data of Path does not work

I want to draw a simple Path which uses RenderedGeometry of a Polygon as Data.
Polygon polygon = new Polygon();
polygon.Points = new PointCollection { new Point(0, 0), new Point(0, 100), new Point(150, 150) };
var path = new Path
{
Data = polygon.RenderedGeometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Panel.SetZIndex(path, 2);
canvas.Children.Add(path);
However my Canvas does not display anything.
You should force the geometry to be rendered before you it to the Canvas. You can do this by calling the Arrange and Measure methods of the Polygon:
Polygon polygon = new Polygon();
polygon.Points = new PointCollection { new Point(0, 0), new Point(0, 100), new Point(150, 150) };
polygon.Arrange(new Rect(canvas.RenderSize));
polygon.Measure(canvas.RenderSize);
var path = new Path
{
Data = polygon.RenderedGeometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Panel.SetZIndex(path, 2);
canvas.Children.Add(path);
You shouldn't be using a Polygon element to define the Geometry of a Path.
Instead directly create a PathGeometry like this:
var figure = new PathFigure
{
StartPoint = new Point(0, 0),
IsClosed = true
};
figure.Segments.Add(new PolyLineSegment
{
Points = new PointCollection { new Point(0, 100), new Point(150, 150) },
IsStroked = true
});
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
var path = new Path
{
Data = geometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Or directly create a Geometry from a string using Path Markup Syntax:
var path = new Path
{
Data = Geometry.Parse("M0,0 L0,100 150,150Z"),
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};

How to draw sector ( Quarter Circle) in windows phone dynamically?

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:

.NET Graphics - creating an ellipse with transparent background

The following code will draw an ellipse on an image and fill that ellipse with the Tomato colour
string imageWithTransEllipsePathToSaveTo = "~/Images/imageTest.png";
Graphics g = Graphics.FromImage(sourceImage);
g.FillEllipse(Brushes.Tomato, 50, 50, 200, 200);
sourceImage.Save(Server.MapPath(imageWithTransEllipsePathToSaveTo), ImageFormat.Png);
If I change the brush to Transparent it obviously will not show because the ellipse will be transparent and the image underneath will show.
How do I set the 'background' of the ellipse to be transparent so that the image contains a transparent spot?
EDIT:
Sorry for the confusion but like this...
This is my second answer and works with an Image instead of a color brush. Unfortunately there is no RadialImageBrush (known to me). I've included code to save the image to the disk, and included usings to ensure you import the correct components. This does use WPF but it should work as part of a library or console app.
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Shapes;
namespace WpfApplication30
{
class ImageEditor
{
public static void processImage(string loc)
{
ImageSource ic = new BitmapImage(new Uri(loc, UriKind.Relative));
ImageBrush brush = new ImageBrush(ic);
Path p = new Path();
p.Fill = brush;
CombinedGeometry cb = new CombinedGeometry();
cb.GeometryCombineMode = GeometryCombineMode.Exclude;
EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 5, 5);
RectangleGeometry rect = new RectangleGeometry(new Rect(new Size(100, 100)));
cb.Geometry1 = rect;
cb.Geometry2 = ellipse;
p.Data = cb;
Canvas inkCanvas1 = new Canvas();
inkCanvas1.Children.Add(p);
inkCanvas1.Height = 96;
inkCanvas1.Width = 96;
inkCanvas1.Measure(new Size(96, 96));
inkCanvas1.Arrange(new Rect(new Size(96, 96)));
RenderTargetBitmap targetBitmap =
new RenderTargetBitmap((int)inkCanvas1.ActualWidth,
(int)inkCanvas1.ActualHeight,
96d, 96d,
PixelFormats.Default);
targetBitmap.Render(inkCanvas1);
using (System.IO.FileStream outStream = new System.IO.FileStream( loc.Replace(".png","Copy.png"), System.IO.FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
encoder.Save(outStream);
}
}
}
}
Here is the result:
You need to create a brush using a semi-transparent color.
You do that with Color.FromArgb(alpha, r, g, b), where alpha sets the opacity.
Example copied from MSDN:
public void FromArgb1(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Transparent red, green, and blue brushes.
SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(120, 0, 255, 0));
SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(120, 0, 0, 255));
// Base and height of the triangle that is used to position the
// circles. Each vertex of the triangle is at the center of one of the
// 3 circles. The base is equal to the diameter of the circles.
float triBase = 100;
float triHeight = (float)Math.Sqrt(3*(triBase*triBase)/4);
// Coordinates of first circle's bounding rectangle.
float x1 = 40;
float y1 = 40;
// Fill 3 over-lapping circles. Each circle is a different color.
g.FillEllipse(trnsRedBrush, x1, y1, 2*triHeight, 2*triHeight);
g.FillEllipse(trnsGreenBrush, x1 + triBase/2, y1 + triHeight,
2*triHeight, 2*triHeight);
g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2*triHeight, 2*triHeight);
}
You need to use a RadialGradientBrush:
RadialGradientBrush b = new RadialGradientBrush();
b.GradientOrigin = new Point(0.5, 0.5);
b.Center = new Point(0.5, 0.5);
b.RadiusX = 0.5;
b.RadiusY = 0.5;
b.GradientStops.Add(new GradientStop(Colors.Transparent,0));
b.GradientStops.Add(new GradientStop(Colors.Transparent,0.25));
b.GradientStops.Add(new GradientStop(Colors.Tomato, 0.25));
g.FillEllipse(b, 50, 50, 200, 200);

create polygon filled up with dots

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;

Categories