2013-01-11 76 views
1

在WPF用戶控件中,我試圖用一個可以在位置和大小上有所不同的圓圈蒙版圖像控件。用戶左拖動改變圓的半徑,右拖動改變橢圓的中心點。我正確捕獲所有需要的值,並可以正確設置蒙版的半徑。問題是,無論我用什麼點來使用橢圓的中心,它都是從圖像控件的中心繪製的。有任何想法嗎?在WPF中定位不透明蒙板

下面是設置屏蔽代碼:

private void SetMask(double _Radius) 
{ 
     EllipseGeometry MaskGeometry = new EllipseGeometry(CenterPos, _Radius, _Radius); 
     GeometryDrawing MaskDrawing = new GeometryDrawing(Brushes.Black, null, MaskGeometry); 
     DrawingBrush MaskBrush = new DrawingBrush(MaskDrawing); 
     MaskBrush.Stretch = Stretch.None; 
     Img.OpacityMask = MaskBrush; //Img is the Image control 
} 

回答

2

您還可以設置ViewboxUnitsAlignmentXAlignmentY性質:

private void SetMask(double radius) 
{ 
    var maskGeometry = new EllipseGeometry(CenterPos, radius, radius); 
    var maskDrawing = new GeometryDrawing(Brushes.Black, null, maskGeometry); 
    var maskBrush = new DrawingBrush 
    { 
     Drawing = maskDrawing, 
     Stretch = Stretch.None, 
     ViewboxUnits = BrushMappingMode.Absolute, 
     AlignmentX = AlignmentX.Left, 
     AlignmentY = AlignmentY.Top 
    }; 

    Img.OpacityMask = maskBrush; 
} 
+0

好的......謝謝!修復它! – magnvs

0

您需要修改ViewportViewportUnits財產您的DrawingBrush瓷磚:

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     EllipseGeometry MaskGeometry = new EllipseGeometry(CenterPos, _RadiusX, _RadiusY); 
     GeometryDrawing MaskDrawing = new GeometryDrawing(Brushes.Black, null, MaskGeometry); 
     DrawingBrush MaskBrush = new DrawingBrush(MaskDrawing); 
     MaskBrush.Stretch = Stretch.None; 
     MaskBrush.ViewportUnits = BrushMappingMode.Absolute; 
     MaskBrush.Viewport = MaskGeometry.Bounds; 
     Img.OpacityMask = MaskBrush; //Img is the Image control 

    } 

MSDN文檔here給出瞭如何使用TileBrush繪製控件的一個很好的解釋。