gpt4 book ai didi

c# - 来自 BitmapImage 的 CroppedImage

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:41 25 4
gpt4 key购买 nike

我试图在 Image 控件中显示图像的固定部分。 Source 是来自磁盘或 Web 资源的 BitmapImage,并且是异步加载的。

我尝试使用 CroppedImage

<UserControl.Resources>
<CroppedBitmap x:Key="croppedImage" Source="{Binding Image}" SourceRect="20 46 273 202"/>
</UserControl.Resources>
...
<Image x:Name="TemplateImage" Height="202" Width="273" HorizontalAlignment="Left" Source="{StaticResource croppedImage}"/>

这会在尝试创建 CroppedBitmap 时产生一个 XamlParseException
我还在后面的代码 (C#) 中尝试过这个

new CroppedBitmap(Image, new System.Windows.Int32Rect(20, 46, 273, 202))

从 Web 资源加载时给我一个 ArgumentException,说明该值超出预期范围。我想这是由于图像尚未加载,因此没有大小。

有没有一种方法可以做到这一点(不一定要使用 CroppedImage),而无需预加载图像?

顺便说一句:将 BitmapImage 直接作为 Image 控件的源可以正常工作,但当然这不会进行裁剪。

最佳答案

您可以使用带有 ImageBrush 填充的 Rectangle 代替 Image 控件,并根据需要设置 Viewbox:

<UserControl.Resources>
<ImageBrush x:Key="croppedImage" ImageSource="{Binding Image}"
ViewboxUnits="Absolute" Viewbox="20,46,273,202"/>
</UserControl.Resources>
...
<Rectangle Height="202" Width="273" Fill="{StaticResource croppedImage}"/>

关于c# - 来自 BitmapImage 的 CroppedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19598833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com