在WPF中使用StreamGeometry创建多个形状时如何更改填充颜色 [英] How to change fill colors when creating multiple shapes using StreamGeometry in WPF

查看:1191
本文介绍了在WPF中使用StreamGeometry创建多个形状时如何更改填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF中使用StreamGeometry来动态创建一堆随机形状.我读到这应该是最好的性能.不幸的是,我无法弄清楚如何独立地改变每种形状的颜色.例如,将以下代码诠释为:

I''m trying to use StreamGeometry in WPF to create a bunch of random shapes on the fly. I read that this was supposed to give the best performance. Unfortunately, I can''t figure out how to change the color on each shape independtly. For example, int the code below:

private void CreateGraphics(){
     int NUM_SHAPES = 100;
     int RECT_MAX_WIDTH = 100;
     int RECT_MAX_HEIGHT = 100;

     StackPanel mainPanel = new StackPanel();

     // Create a StreamGeometry to use to specify myPath.
     StreamGeometry geometry = new StreamGeometry();
     geometry.FillRule = FillRule.EvenOdd;


     using (StreamGeometryContext context = geometry.Open())
     {

         for (int i = 0; i < NUM_SHAPES; i++)
         {

             // Create a path to draw a geometry with.
             Path myPath = new Path();

             // Get random height, width, position, and color.
             double width = RECT_MAX_WIDTH * RAND.NextDouble();
             double height = RECT_MAX_HEIGHT * RAND.NextDouble();
             int x = (int)(SCREEN_WIDTH * RAND.NextDouble());
             int y = (int)(SCREEN_HEIGHT * RAND.NextDouble());

             // Create a color.
             int fill_color_num = RAND.Next(1, MAX_COLORS);
             Color fill_color = Color.FromArgb(0xFF, //alpha
                             (byte)(fill_color_num & 0xFF), //red
                             (byte)((fill_color_num >> 8) & 0xFF), //green
                             (byte)((fill_color_num >> 16) & 0xFF)); //blue
             int edge_color_num = RAND.Next(1, MAX_COLORS);
             Color edge_color = Color.FromArgb(0xFF, //alpha
                             (byte)(edge_color_num & 0xFF), //red
                             (byte)((edge_color_num >> 8) & 0xFF), //green
                             (byte)((edge_color_num >> 16) & 0xFF)); //blue

             myPath.StrokeThickness = 2;
             //myPath.Stroke = Brushes.Black;
             //myPath.Fill = Brushes.Yellow;
             myPath.Stroke = new SolidColorBrush(edge_color);
             myPath.Fill = new SolidColorBrush(fill_color);

             // Set Edges.
             int left_edge = x - (int)(width / 2);
             int right_edge = x + (int)(width / 2);
             int top_edge = y - (int)(height / 2);
             int bottom_edge = y + (int)(height / 2);
             if (left_edge < 0) { left_edge = 0; }
             if (right_edge > SCREEN_WIDTH) { right_edge = SCREEN_WIDTH; }
             if (top_edge < 0) { top_edge = 0; }
             if (bottom_edge > SCREEN_HEIGHT) { bottom_edge = SCREEN_HEIGHT; }

             // Begin the triangle at the point specified. Notice that the shape is set to
             // be closed so only two lines need to be specified below to make the triangle.
             context.BeginFigure(new Point(left_edge, bottom_edge), true /*isFilled*/, true /*isClosed*/);

             // Draw a line to the next specified point.
             //context.LineTo(new Point(left_edge, top_edge), true /* is stroked */, false /* is smooth join */);
             context.LineTo(new Point(left_edge, top_edge), true /*isStroked*/, true /*isSmoothJoin*/);

             // Draw another line to the next specified point.
             //context.LineTo(new Point(right_edge, top_edge), true /* is stroked */, false /* is smooth join */);
             context.LineTo(new Point(right_edge, top_edge), true /*isStroked*/, true /*isSmoothJoin*/);

             // Draw another line to the next specified point.
             //context.LineTo(new Point(right_edge, bottom_edge), true /* is stroked */, false /* is smooth join */);
             context.LineTo(new Point(right_edge, bottom_edge), true /*isStroked*/, true /*isSmoothJoin*/);

             // Freeze the geometry (make it unmodifiable)
             // for additional performance benefits.
             geometry.Freeze();

             // Specify the shape (triangle) of the Path using the StreamGeometry.
             myPath.Data = geometry;

             // Add path shape to the UI.
             mainPanel.Children.Add(myPath);
         }
     }

    this.Content = mainPanel;
}



我期望通过设置Path.Fill和Path.Stroke属性,我将为每个矩形获得不同的颜色.看来我只能将最后定义的颜色应用于所有矩形.有谁知道如何为每个矩形设置唯一的颜色?

我尝试将StreamGeometry对象声明放置在for循环内,正如一位用户建议的那样.由于某种原因,我只能得到2个形状.这是代码:



I was expecting that by setting the Path.Fill and Path.Stroke properties, that I would get different colors for each rectangle. It seems that I only get the last defined color applied to all the rectangles. Does anyone know how to set unique colors for each rectangle?

I tried placing the StreamGeometry object declaration inside the for loop, as one user suggested. For some reason I only get 2 shapes. Here is the code:

private void CreateGraphics()
 {
     int NUM_SHAPES = 3;
     int RECT_MAX_WIDTH = 100;
     int RECT_MAX_HEIGHT = 100;
     StackPanel mainPanel = new StackPanel();

     for (int i = 0; i < NUM_SHAPES; i++)
     {

         // Create a StreamGeometry to use to specify myPath.
         StreamGeometry geometry = new StreamGeometry();
         geometry.FillRule = FillRule.EvenOdd;

         using (StreamGeometryContext context = geometry.Open())
         {

                 // Create a path to draw a geometry with.
             Path myPath = new Path();

             // Get random height, width, position, and color.
             double width = RECT_MAX_WIDTH * RAND.NextDouble();
             double height = RECT_MAX_HEIGHT * RAND.NextDouble();
             int x = (int)(SCREEN_WIDTH * RAND.NextDouble());
             int y = (int)(SCREEN_HEIGHT * RAND.NextDouble());

             // Create a color.
             int fill_color_num = RAND.Next(1, MAX_COLORS);
             Color fill_color = Color.FromArgb(0xFF, //alpha
                             (byte)(fill_color_num & 0xFF), //red
                             (byte)((fill_color_num >> 8) & 0xFF), //green
                             (byte)((fill_color_num >> 16) & 0xFF)); //blue
             int edge_color_num = RAND.Next(1, MAX_COLORS);
             Color edge_color = Color.FromArgb(0xFF, //alpha
                             (byte)(edge_color_num & 0xFF), //red
                             (byte)((edge_color_num >> 8) & 0xFF), //green
                             (byte)((edge_color_num >> 16) & 0xFF)); //blue

             myPath.StrokeThickness = 2;
             //myPath.Stroke = Brushes.Black;
             //myPath.Fill = Brushes.Yellow;
             myPath.Stroke = new SolidColorBrush(edge_color);
             myPath.Fill = new SolidColorBrush(fill_color);

             // Set Edges.
             int left_edge = x - (int)(width / 2);
             int right_edge = x + (int)(width / 2);
             int top_edge = y - (int)(height / 2);
             int bottom_edge = y + (int)(height / 2);
             if (left_edge < 0) { left_edge = 0; }
             if (right_edge > SCREEN_WIDTH) { right_edge = SCREEN_WIDTH; }
             if (top_edge < 0) { top_edge = 0; }
             if (bottom_edge > SCREEN_HEIGHT) { bottom_edge = SCREEN_HEIGHT; }

             // Begin the triangle at the point specified. Notice that the shape is set to
             // be closed so only two lines need to be specified below to make the triangle.
             context.BeginFigure(new Point(left_edge, bottom_edge), true /*isFilled*/, true /*isClosed*/);

             // Draw a line to the next specified point.
             //context.LineTo(new Point(left_edge, top_edge), true /* is stroked */, false /* is smooth join */);
             context.LineTo(new Point(left_edge, top_edge), true /*isStroked*/, true /*isSmoothJoin*/);

             // Draw another line to the next specified point.
             //context.LineTo(new Point(right_edge, top_edge), true /* is stroked */, false /* is smooth join */);
             context.LineTo(new Point(right_edge, top_edge), true /*isStroked*/, true /*isSmoothJoin*/);

             // Draw another line to the next specified point.
             //context.LineTo(new Point(right_edge, bottom_edge), true /* is stroked */, false /* is smooth join */);
             context.LineTo(new Point(right_edge, bottom_edge), true /*isStroked*/, true /*isSmoothJoin*/);

             // Freeze the geometry (make it unmodifiable)
             // for additional performance benefits.
             geometry.Freeze();

             // Specify the shape (triangle) of the Path using the StreamGeometry.
             myPath.Data = geometry;

             // Add path shape to the UI.
             mainPanel.Children.Add(myPath);
         }
     }

    this.Content = mainPanel;
 }

推荐答案

您正在对所有Path对象使用相同的几何对象,最后一个将位于其他对象的顶部,您将只会看到一个让您认为所有路径都使用相同颜色的东西.
You''re using the same geometry object for all the Path objects, the last one will stay on top of the others and you will only see that one which is making you think that all the Path are using the same colour.


这篇关于在WPF中使用StreamGeometry创建多个形状时如何更改填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆