C# 将WPF画布中的图像居中对齐
我正在使用Kinect开发一个基于手势的图像查看器。我使用以下XAML代码在画布上显示图像C# 将WPF画布中的图像居中对齐,c#,wpf,xaml,canvas,C#,Wpf,Xaml,Canvas,我正在使用Kinect开发一个基于手势的图像查看器。我使用以下XAML代码在画布上显示图像 <Canvas Background="Transparent" Height="732" VerticalAlignment="Top"> <Image x:Name="next" Source="{Binding NextPicture}" StretchDirection="Both" Stretch="Uniform" ClipToBounds="True
<Canvas Background="Transparent" Height="732" VerticalAlignment="Top">
<Image x:Name="next" Source="{Binding NextPicture}" StretchDirection="Both" Stretch="Uniform" ClipToBounds="True" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="HighQuality" RenderTransformOrigin="0.5,0.5" Opacity="0" Grid.ColumnSpan="2" Height="732" Width="Auto" HorizontalAlignment="Center" />
<Image x:Name="previous" Source="{Binding PreviousPicture}" StretchDirection="Both" Stretch="Uniform" ClipToBounds="True" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="HighQuality" RenderTransformOrigin="0.5,0.5" Opacity="0" Grid.ColumnSpan="2" Height="732" Width="Auto" HorizontalAlignment="Center" />
<Image x:Name="current" Source="{Binding Picture}" StretchDirection="Both" Stretch="Uniform" ClipToBounds="True" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="HighQuality" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="2" Height="732" Width="Auto" HorizontalAlignment="Center" />
</Canvas>
但问题是,我在画布上显示的图像会随机向左或向右对齐。它们的纵横比有时也会发生变化。我想要的是,图像应该以其原始纵横比显示在画布的中心。在这方面有人能帮我吗
此外,我还可以使用C代码将图像设置为中心,如下所示:
Canvas.SetLeft(current, (1249 - current.ActualWidth) / 2);
Canvas.SetTop(current, 0);
其中,“current”是XAML代码中当前图像的名称。但我希望它能自动发生。在XAML代码中可能吗?如果没有,我如何实现它
附言:我不太擅长C#或WPF。因此,请尽可能用外行术语解释。您可以通过使用接受
Canvas.Width
和image.Width
参数并返回(Canvas.Width/2)-(image.Width/2)
的,但是,根据上面的说明,您似乎允许用户通过设置图像的Canvas.Left
,使用手的移动来重新定位图像,这将覆盖您与转换器的绑定
实际上,我建议使用网格
而不是画布
,并使用水平对齐=中心
定位图像,并允许用户使用渲染变换来调整图像的位置
(请注意,您需要使用RenderTransform
,而不是LayoutTransform
,以便在渲染时应用转换,而不是在WPF尝试布局对象时)
如果这不起作用,我会将图片属性从BitmapImages更改为表示该图像的实际类,带有
BitmapImage
、Top
和Left
的属性。要在图像周围移动,您需要更改Picture.Left
属性,当用户切换图片时,您可以重置Canvas.Left
属性,使用接受Canvas.Width
和image.Width
参数并返回(Canvas.Width/2)-(image.Width/2)的
,但是根据上面的内容,您似乎允许用户通过设置图像的Canvas.Left
,使用手移动来重新定位图像,这将覆盖您与转换器的绑定
实际上,我建议使用网格
而不是画布
,并使用水平对齐=中心
定位图像,并允许用户使用渲染变换来调整图像的位置
(请注意,您需要使用RenderTransform
,而不是LayoutTransform
,以便在渲染时应用转换,而不是在WPF尝试布局对象时)
如果这不起作用,我会将图片属性从BitmapImages更改为表示该图像的实际类,带有
BitmapImage
、Top
和Left
的属性。要在图像周围移动,您需要更改Picture.Left
属性,当用户切换图片时,您可以重置画布
来包含控件有什么原因吗?a网格
会更有效b因为您可以简单地将图像的水平对齐
和垂直对齐
设置为中心
@Rachel,是的。实际上,我正在将手坐标转换为相对于屏幕的位置,然后通过设置代码中的“Canvas.Left”和“Canvas.Top”属性来平移缩放图像。我ried使用了“Translate Transform”,但我想不出使用它的方法。你为什么要使用画布
来包含控件?网格会更好,因为你可以简单地将图像的水平对齐
和垂直对齐
设置为中心
@Rachel,是的。我是我实际上是将我的手坐标转换成相对于屏幕的位置,然后通过设置代码中的“Canvas.Left”和“Canvas.Top”属性来平移缩放的图像。我尝试使用“Translate Transform”,但我想不出使用它的方法。我在使用渲染变换时犯了一个愚蠢的错误。现在可以了。谢谢提示:)我在使用渲染变换时犯了一个愚蠢的错误。它现在可以工作了。谢谢提示:)
// Setup corresponding picture if pictures are available.
this.PreviousPicture = this.Picture;
this.Picture = this.NextPicture;
this.NextPicture = LoadPicture(Index + 1);
// Notify world of change to Index and Picture.
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("PreviousPicture"));
this.PropertyChanged(this, new PropertyChangedEventArgs("Picture"));
this.PropertyChanged(this, new PropertyChangedEventArgs("NextPicture"));
}
Canvas.SetLeft(current, (1249 - current.ActualWidth) / 2);
Canvas.SetTop(current, 0);