im试图创建一个包含“浏览”按钮以加载图像文件和“ +”按钮以放大图像的窗口。浏览按钮及其打开的对话框将图像成功加载到Image aka中。当我按下“ +”按钮时,它不会放大加载的图像,但会使其模糊。怎么了?
原始加载的图像被命名为OriginalSizedImage。 zoomedIn图像命名为
private void ZoomInButton_Click(object sender, RoutedEventArgs e)
{
BitmapImage newZoomedInImage = new BitmapImage();
newZoomedInImage.BeginInit();
newZoomedInImage.DecodePixelWidth = originalSizedImage.DecodePixelWidth + 20 ;
newZoomedInImage.DecodePixelHeight = originalSizedImage.DecodePixelHeight + 20;
newZoomedInImage.UriSource = originalSizedImage.UriSource;
newZoomedInImage.EndInit();
imageView.Source = newZoomedInImage ;
}
答案 0 :(得分:3)
每次更改缩放系数时,创建新的BitmapImage都是不必要且效率低下的。
最好将Image元素放置在对其进行裁剪的父元素中
<Grid ClipToBounds="True">
<Image x:Name="imageView" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<ScaleTransform/>
</Image.RenderTransform>
</Image>
</Grid>
,然后修改分配给其ScaleTransform
(可能还有其RenderTransform
)的RenderTransformOrigin
:
private void ZoomInButtonClick(object sender, RoutedEventArgs e)
{
var transform = (ScaleTransform)imageView.RenderTransform;
transform.ScaleX *= 1.1;
transform.ScaleY *= 1.1;
}
private void ZoomOutButtonClick(object sender, RoutedEventArgs e)
{
var transform = (ScaleTransform)imageView.RenderTransform;
transform.ScaleX /= 1.1;
transform.ScaleY /= 1.1;
}