views:

65

answers:

1

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);
+1  A: 

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
@ 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???
deep
@ 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
Yeah got working Torsten, Thanks a loot
deep
@ 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???
deep
@deep could you update your question with the code you are using currently to see how it is that you are saving the image?
Limo Wan Kenobi
@Limo Wan Kenobi deep asked a new question for this problem: http://stackoverflow.com/questions/3509296/wpf-polygon-to-bitmap
Torsten