WPF - 如何对裁剪后的图像应用效果?

发布于 2024-08-10 22:21:14 字数 374 浏览 7 评论 0原文

我有一个 Image 被剪裁如下:

<Image Width="45" Grid.Column="0" Source="{Binding Photo}">
    <Image.Clip>
        <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" />
    </Image.Clip>
</Image>

如何对其应用投影效果?

更新:
正如 Ray 所指出的,最好的解决方案是 Anderson 提出的方案 - 具有环绕边框。谢谢安德森。

I have an Image being clipped like so:

<Image Width="45" Grid.Column="0" Source="{Binding Photo}">
    <Image.Clip>
        <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" />
    </Image.Clip>
</Image>

How can I apply a drop shadow effect to it?

UPDATE:
As pointed out by Ray, the best solution is the one proposed by Anderson - having a wrapping border. Thanks Anderson.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

垂暮老矣 2024-08-17 22:21:14

这将为您解决问题:

<Border>
  <Border.Effect>
    <DropShadowEffect />
  </Border.Effect>
  <Image Stretch="None" Source="{Binding Photo}" >
    <Image.Clip>
      <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8"/>
    </Image.Clip>
  </Image>
</Border>

这当然是您最初的想法,只是将 DropShadowEffect 应用于环绕边框。由于位图效果的工作方式,它们仅适用于所包含内容的可见部分。

This will do the trick for you:

<Border>
  <Border.Effect>
    <DropShadowEffect />
  </Border.Effect>
  <Image Stretch="None" Source="{Binding Photo}" >
    <Image.Clip>
      <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8"/>
    </Image.Clip>
  </Image>
</Border>

Which of course is your original idea, only with the DropShadowEffect applied to a wrapping Border. Because of the way bitmap effects work, they apply only to the visible part of what is contained.

骷髅 2024-08-17 22:21:14

这应该可行,

<Image Width="45" Grid.Column="0" Source="{Binding Photo}" 
    <Image.Clip>
        <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" />
    </Image.Clip>
    <Image.Effect>
        <DropShadowEffect Color="Black" BlurRadius="20" />
    </Image.Effect>
</Image>

不过我还没有尝试过与 Clip 结合使用。

更新:这不起作用(似乎是一个错误?)

我只是这样做:

<Border Grid.Column="0" >
     <Border.Effect>
          <DropShadowEffect />
     </Border.Effect>
    <Image Width="45" Source="{Binding Photo}" 
        <Image.Clip>
            <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" />
        </Image.Clip>
    </Image>
</Border>

有点蹩脚,你可能需要调整一些宽度以确保它们完全匹配,但你明白了。

This should work

<Image Width="45" Grid.Column="0" Source="{Binding Photo}" 
    <Image.Clip>
        <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" />
    </Image.Clip>
    <Image.Effect>
        <DropShadowEffect Color="Black" BlurRadius="20" />
    </Image.Effect>
</Image>

I've not tried it in combination with Clip, though.

Update: That doesn't work (seems like a bug?)

I'd just do this:

<Border Grid.Column="0" >
     <Border.Effect>
          <DropShadowEffect />
     </Border.Effect>
    <Image Width="45" Source="{Binding Photo}" 
        <Image.Clip>
            <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" />
        </Image.Clip>
    </Image>
</Border>

Bit lame and you might have to tweak some of the widths to make sure they match exactly, but you get the idea.

無心 2024-08-17 22:21:14

我想答案是我需要使用 CroppedBitmap 而不是 Image.Clip:

<Image Width="45">
    <Image.Source>
        <CroppedBitmap Source="{Binding Photo}" SourceRect="0 0 45 55"/>
    </Image.Source>
    <Image.Effect>
        <DropShadowEffect/>
    </Image.Effect>
</Image>

如果我需要圆角,我可以用边框包围外部图像并使用 ImageBrush:

<Border Width="45" Height="55" CornerRadius="10" >
    <Border.Background>
        <ImageBrush>
            <ImageBrush.ImageSource>
                <CroppedBitmap Source="profile.jpg" SourceRect="0 0 45 55"/>
            </ImageBrush.ImageSource>
        </ImageBrush>    
    </Border.Background>
</Border>

如果我错了,请纠正我,或者你可以这样做以更简单的方式。
谢谢!

更新:显然您无法绑定到 CroppedBitmap 的 Source 属性!

I guess the answer is that I need to use CroppedBitmap instead of Image.Clip:

<Image Width="45">
    <Image.Source>
        <CroppedBitmap Source="{Binding Photo}" SourceRect="0 0 45 55"/>
    </Image.Source>
    <Image.Effect>
        <DropShadowEffect/>
    </Image.Effect>
</Image>

and if I need the round corners I can surround the outer image with a border and use a ImageBrush:

<Border Width="45" Height="55" CornerRadius="10" >
    <Border.Background>
        <ImageBrush>
            <ImageBrush.ImageSource>
                <CroppedBitmap Source="profile.jpg" SourceRect="0 0 45 55"/>
            </ImageBrush.ImageSource>
        </ImageBrush>    
    </Border.Background>
</Border>

Please correct me if I am wrong or you can do it in a simpler manner.
Thanks!

UPDATE: Apparently you cannot bind to CroppedBitmap's Source property!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文