DrawingVisual:如何清除区域? [英] DrawingVisual: How to clear a region?
本文介绍了DrawingVisual:如何清除区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
情况:在DrawingVisual上绘制白色矩形。
目标:清除它的一部分(几何)以使其透明。
Windows窗体替代:Graphics.Clear 。
代码:
public 部分 类 MainWindow:Window
{
public MainWindow ()
{
InitializeComponent();
visuals = new VisualCollection( this );
DrawIt();
}
VisualCollection视觉效果;
void DrawIt()
{
DrawingVisual visual = new DrawingVisual();
使用(DrawingContext dc = visual.RenderOpen())
{
dc.DrawRectangle(
Brushes.White, new Pen(),
new Rect( 10 , 10 , 100 , 100 < /跨度>));
dc.DrawRectangle(
Brushes.Red, new Pen(),
new Rect( 50 , 50 , 20 , 20 )); // 将其删除
}
visuals.Add(visual);
}
protected 覆盖 Visual GetVisualChild( int index)
{
return visuals [index];
}
受保护 覆盖 int VisualChildrenCount
{
get { return visuals 。计数; }
}
}
解决方案
这可以用不同的方式呈现。我建议如下:定义一些非矩形形状的Geometry
并将实例分配给属性System.Windows.Media.Visual.VisualClip
:
https://msdn.microsoft.com/en-us/library/system.windows.media.visual.visualclip%28v=vs.110%29.aspx [ ^ ]。
如果你想要Visual
中的透明圆孔,请创建一个Geometry
的实例,里面有一个洞:
https://msdn.microsoft.com/en-us/library/system.windows.media.geometry%28v=vs.110 %29.aspx [ ^ ]。
你可以使用派生类,如System.Windows.Media.PathGeometry
或System.Windows.Media.StreamGeometry
基于某些定义的几何任意路径或StreamGeometryContext
:
https://msdn.microsoft.com/en-us/library/system.windows.media.pathgeometry%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.windows.media.streamgeometrycontext%28v=vs.110%29.aspx [<一个HREF =https://msdn.microsoft.com/en-us/library/system.windows.media.streamgeometrycontext%28v=vs.1 10%29.aspxtarget =_ blanktitle =New Window> ^ ]。
-SA
1。创建2个几何图形 - 绘图区域和要清除的区域。
2.使用DrawingContext的PushClip剪切由排除模式组合的前2个组合几何体。
警告:使用GetOutlinedPathGeometry()!可以在没有它的情况下完成操作,但是如果不使用它,它将获得具有复杂几何形状的更多工件。无论如何,工件都会出现,但比没有GetOutlinedPathGeometry()要少得多。
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
visuals = new VisualCollection( this );
DrawIt();
}
VisualCollection视觉效果;
void DrawIt()
{
DrawingVisual visual = new DrawingVisual();
使用(DrawingContext dc = visual.RenderOpen())
{
RectangleGeometry rect1 = new RectangleGeometry();
rect1.Rect = new Rect( 10 , 10 , 100 , 100 );
RectangleGeometry rect2 = new RectangleGeometry();
rect2.Rect = new Rect( 50 , 50 , 20 , 20 ); // 删除它
Geometry geom1 = rect1.GetOutlinedPathGeometry();
Geometry geom2 = rect2.GetOutlinedPathGeometry();
// Geometry combined1 = new CombinedGeometry(GeometryCombineMode.Exclude,rect1,rect2); //不推荐
Geometry combined2 = new CombinedGeometry(GeometryCombineMode.Exclude,geom1,geom2);
dc.PushClip(combined2);
dc.DrawRectangle(
Brushes.White, new Pen(),
new Rect( 10 , 10 , 100 , 100 ));
}
visuals.Add(visual);
}
protected 覆盖 Visual GetVisualChild( int index)
{
return visuals [index];
}
受保护 覆盖 int VisualChildrenCount
{
get { return visuals 。计数; }
}
}
Situation: White rectangle is drawn on DrawingVisual.
Goal: Clear a part (geometry) of it to make it transparent.
Windows Forms alternative: Graphics.Clear.
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
visuals = new VisualCollection(this);
DrawIt();
}
VisualCollection visuals;
void DrawIt()
{
DrawingVisual visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
dc.DrawRectangle(
Brushes.White, new Pen(),
new Rect(10, 10, 100, 100));
dc.DrawRectangle(
Brushes.Red, new Pen(),
new Rect(50, 50, 20, 20)); // Remove it
}
visuals.Add(visual);
}
protected override Visual GetVisualChild(int index)
{
return visuals[index];
}
protected override int VisualChildrenCount
{
get { return visuals.Count; }
}
}
解决方案
This can be rendered in different ways. I would suggest the following: define someGeometry
of non-rectangular shape and assign the instance to the propertySystem.Windows.Media.Visual.VisualClip
:
https://msdn.microsoft.com/en-us/library/system.windows.media.visual.visualclip%28v=vs.110%29.aspx[^].
Say, if you want a transparent round hole in yourVisual
, create an instance ofGeometry
with the hole in it:
https://msdn.microsoft.com/en-us/library/system.windows.media.geometry%28v=vs.110%29.aspx[^].
You can use derived classes likeSystem.Windows.Media.PathGeometry
orSystem.Windows.Media.StreamGeometry
to defined geometry based on some arbitrary path orStreamGeometryContext
:
https://msdn.microsoft.com/en-us/library/system.windows.media.pathgeometry%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.media.streamgeometrycontext%28v=vs.110%29.aspx[^].
—SA
1. Create 2 geometries - a drawing area and an area to clear.
2. Use PushClip of DrawingContext to clip the combined geometry of the first 2 ones combined by Exclude mode.
Warning: Use GetOutlinedPathGeometry()! Operations may be done without it too, but it will get more artifacts with complex geometries, if you don't use it. Artifacts will appear by anyway, but much more less than without GetOutlinedPathGeometry().
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); visuals = new VisualCollection(this); DrawIt(); } VisualCollection visuals; void DrawIt() { DrawingVisual visual = new DrawingVisual(); using (DrawingContext dc = visual.RenderOpen()) { RectangleGeometry rect1 = new RectangleGeometry(); rect1.Rect = new Rect(10, 10, 100, 100); RectangleGeometry rect2 = new RectangleGeometry(); rect2.Rect = new Rect(50, 50, 20, 20); // Removed it Geometry geom1 = rect1.GetOutlinedPathGeometry(); Geometry geom2 = rect2.GetOutlinedPathGeometry(); //Geometry combined1 = new CombinedGeometry(GeometryCombineMode.Exclude, rect1, rect2); // Not recommended Geometry combined2 = new CombinedGeometry(GeometryCombineMode.Exclude, geom1, geom2); dc.PushClip(combined2); dc.DrawRectangle( Brushes.White, new Pen(), new Rect(10, 10, 100, 100)); } visuals.Add(visual); } protected override Visual GetVisualChild(int index) { return visuals[index]; } protected override int VisualChildrenCount { get { return visuals.Count; } } }
这篇关于DrawingVisual:如何清除区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文