我试图在Image控件中显示图像的固定部分。源是来自磁盘或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}"/>这将在尝试创建XamlParseException时生成CroppedBitmap。
我还在后面的代码(C#)中尝试了这一点。
new CroppedBitmap(Image, new System.Windows.Int32Rect(20, 46, 273, 202))在从web资源加载时给我一个ArgumentException,说明该值超出了预期范围。我想这是由于图像还没有加载,因此没有大小。
有什么方法可以做到这一点(不一定用CroppedImage),而不必预先加载映像?
顺便说一句:直接将BitmapImage作为Image控件的源可以很好地工作,但这当然不适用于裁剪。
发布于 2013-10-25 21:24:19
您可以使用带有ImageBrush填充的矩形来代替图像控件,并根据需要设置视图框:
<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}"/>发布于 2013-10-25 21:12:21
您可以做的是创建一个虚拟源代码,它只是一个空白位图:
var src = BitmapImage.Create(
500, // set to be big enough so it can be cropped
500, // set to be big enough so it can be cropped
96,
96,
System.Windows.Media.PixelFormats.Indexed1,
new BitmapPalette(new List<System.Windows.Media.Color> { System.Windows.Media.Colors.Transparent }),
new byte[] { 0, 0, 0, 0 },
1);将绑定为CroppedBitmap源的图像属性设置为此虚拟。然后,当实际的图像加载时,您只需将这个假人替换为真实的一个。当您将Image属性设置为新源时,绑定系统应该负责更新它。
https://stackoverflow.com/questions/19598833
复制相似问题