简体   繁体   English

Silverlight中Image.OpacityMask上的矩形

[英]Rectangle on Image.OpacityMask in Silverlight

I want add Rectangle for Image.OpacityMask in WinPhone. 我想在WinPhone中为Image.OpacityMask添加Rectangle。

It is very easy on WPF: 在WPF上非常简单:

<Image 
      Grid.Row="4" Grid.Column="5"
      Height="150"
      Width="200"
      Source="sampleImages/Waterlilies.jpg">
  <Image.OpacityMask>
    <DrawingBrush>
      <DrawingBrush.Drawing>
        <GeometryDrawing>
          <GeometryDrawing.Geometry>
            <RectangleGeometry Rect="0.05,0.05 0.9,0.9" />
          </GeometryDrawing.Geometry>
      </GeometryDrawing>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Image.OpacityMask>
</Image>

But on WinPhone we can't use DrawingBrush. 但是在WinPhone上,我们不能使用DrawingBrush。

How to add Rectangle on OpasityMask for Image on WinPhone? 如何在WinPhone的OpasityMask上为图像添加Rectangle?

It seem there that you are just trying to just clip the image so you could just use the Clip property which you can set to a geometry. 似乎您只是在尝试剪切图像,因此可以使用可以设置为几何体的Clip属性。
If you are in fact trying to achieve something more complex, than what you could do is use a WriteableBitmap to render the Geometry into a Bitmap and use the writeable bitmap in the OpacityMask. 如果实际上您想实现更复杂的功能,则可以使用WriteableBitmap将Geometry渲染为位图,并在OpacityMask中使用可写位图。 The following attached property can help you achieve that: 以下附加属性可以帮助您实现:

public class MyAttached
{

    public static readonly DependencyProperty GeometryOpacityMaskProperty =
        DependencyProperty.RegisterAttached("GeometryOpacityMask", typeof(Path), typeof(MyAttached), new PropertyMetadata(default(Path), GeometryOpacityMaskChanged));

    private static void GeometryOpacityMaskChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement uiElement = d as FrameworkElement;

        Path path = e.NewValue as Path;
        if (path != null && uiElement != null)
        {
            WriteableBitmap writeableBitmap = new WriteableBitmap((int)uiElement.Width, (int)uiElement.Height);
            writeableBitmap.Render(path, new CompositeTransform());
            writeableBitmap.Invalidate();

            ImageBrush imageBrush=new ImageBrush();
            imageBrush.ImageSource = writeableBitmap;
            uiElement.OpacityMask = imageBrush;
        }
    }

    public static void SetGeometryOpacityMask(UIElement element, Path value)
    {
        element.SetValue(GeometryOpacityMaskProperty, value);
    }

    public static Path GetGeometryOpacityMask(UIElement element)
    {
        return (Path)element.GetValue(GeometryOpacityMaskProperty);
    }
}

That you can use like this: 您可以这样使用:

<Image 

  Height="150"
  Width="200"
  Source="sampleImages/Waterlilies.jpg"
        Stretch="UniformToFill"
        >
        <phoneApp1:MyAttached.GeometryOpacityMask>
            <Path>
                <Path.Data>
                    <RectangleGeometry Rect="0.05,0.05 0.9,0.9" />
                </Path.Data>
            </Path>
        </phoneApp1:MyAttached.GeometryOpacityMask>
    </Image>

Use Image.Clip for cut of part Image. 使用Image.Clip剪切零件图像。

Clip Ellipse: 剪辑椭圆:

<Image Name="Img" Source="/UntitledImage.jpg">
  <Image.Clip>
    <EllipseGeometry Center="115,115" RadiusX="50" RadiusY="50"></EllipseGeometry>
  </Image.Clip>
</Image>

Clip Rectangle: 剪辑矩形:

<Image Name="oldImg" Source="/UntitledImage.jpg">
  <Image.Clip>
    <RectangleGeometry Rect="115,115,50,50"></RectangleGeometry>
  </Image.Clip>
</Image>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM