2

alt textAm 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);
Spen D
  • 4,225
  • 9
  • 39
  • 47

1 Answers1

3

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;
Torsten
  • 1,696
  • 2
  • 21
  • 42
  • @ Torsten,not in order, just a dotted pattern thats all, and am not using any XAML code here, just want to create that image and save as to bitmap??? – Spen D Aug 17 '10 at 08:16
  • @ deep: I edited my post to give you a code example. However, as to saving this polygon as bitmap I can't give you much advice. Maybe this link can help you out, you pass your Polygon in as the Visual parameter: http://www.wpftutorial.net/BitmapFromVisual.html – Torsten Aug 17 '10 at 09:13
  • @ Torsten, i tried to send as visual parameter and it works, but the saved image was blank... plz can you tell me how to convert it to bitmap or memory strem??? – Spen D Aug 18 '10 at 08:52
  • @deep could you update your question with the code you are using currently to see how it is that you are saving the image? – Edgar Hernandez Aug 19 '10 at 18:06
  • @Limo Wan Kenobi deep asked a new question for this problem: http://stackoverflow.com/questions/3509296/wpf-polygon-to-bitmap – Torsten Aug 20 '10 at 06:25