Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为我的矩形(drawingcontext)设置不透明度_C#_Wpf_2d_Drawingcontext - Fatal编程技术网

C# 如何为我的矩形(drawingcontext)设置不透明度

C# 如何为我的矩形(drawingcontext)设置不透明度,c#,wpf,2d,drawingcontext,C#,Wpf,2d,Drawingcontext,这是我的矩形 protected void DrawRectangle(DrawingContext dc, Point point) { DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawContext = drawingVisual.RenderOpen()) { Pen

这是我的矩形

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

            }
        }
所以我的问题是如何设置不透明度,这是正确的方法吗?

(这是更改矩形的不透明度)

不要将笔刷.Red传递到矩形中,而是创建一个新的SolidColorBrush,并设置传递到矩形中的SolidColorBrush的不透明度

SolidColorBrush rectBrush = new SolidColorBrush(Colors.Red);
rectBrush.Opacity = 0.5; // or whatever

dc.DrawRectangle(rectBrush, ...
您只需对笔执行类似的操作

drawingVisual.Opacity = 0.5;

只是一张纸条。当您从未使用返回的DrawingContext时,创建DrawingVisual并调用其RenderRopen方法是毫无意义的。