WPF如何将Image.Source居中显示

9

我正在开发一个自定义的WPF .NET 3.5和Visual Studio 2010中的图像控件。

在WinForms中,PictureBox控件有SizeMode属性,其中包括“CenterImage”。

我希望我的图像控件也有这个能力。

有没有办法实现?

谢谢

我的XAML代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="768" Width="1024" xmlns:my="http://schemas.sharpsoft.net/xaml" xmlns:my1="clr-namespace:WpfApplication1">
    <Grid>
        <my1:CustomControl1
                    x:Name="customControl11"
                    Width="206"
                    Height="197"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Margin="18,58,0,0"
                    Stretch="Uniform"/>
    </Grid>
</Window>

我的自定义控件代码:

public class CustomControl1 : Image
{
    public CustomControl1()
    {
        // Bitmap to Stream
        Stream ms = new MemoryStream();
        Properties.Resources.webcam_background.Save(ms, ImageFormat.Png);

        // Stream to BitmapImage
        BitmapImage bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.StreamSource = ms;
        bitmap.EndInit();

        // Set it
        Source = bitmap;
    }
}

"webcam_backgroud" 是默认 Visual Studio 资源编辑器添加的 png 图像。

4个回答

8
你应该尝试使用对齐方式来使整个 图片 元素居中:
    <Grid>
        <Image Stretch="None"
               HorizontalAlignment="Center"
               VerticalAlignment="Center" />
    </Grid>

是的,我做到了。但出于某种原因,控件显示的是一种“缩略图”,而不是原始图像。我在考虑这可能是因为我加载资源的方式不对,没有使用统一资源标识符(URI)。 - JoanComasFdz

8

Stretch 设置为无。

<Image Source="..." Stretch="None" />

我已经尝试过了,但效果并不如我所期望的那样。目前为止,它会大大减小我的图像并将其放在左上角。无:http://img28.imageshack.us/img28/1783/imagestretchnone.png 均匀:http://img810.imageshack.us/img810/2401/imagestretchuniform.png - JoanComasFdz
@unexpectedkas: 能否编辑您的问题并包含一些XAML代码? 默认情况下,图像将在Image控件中居中绘制,但Image控件可能未居中或未延伸到其父级。 例如,如果整个窗口是一个只有一个Image的Grid,则它将居中,但如果是一个仅有一个Image的Canvas,则它将位于左上角。 - Quartermeister
好的,我刚刚做到了。我将水平/垂直对齐设置为居中,是的,图像居中了。非常感谢。不过,为了使其看起来正确,拉伸必须是“均匀”的。我很困惑,因为控件移动到了窗口上。 - JoanComasFdz

0

这是我的工作解决方案:

<Image Name="PreviewImage" 
       HorizontalAlignment="Stretch" 
       Stretch="Uniform" 
       VerticalAlignment="Top" 
       Width="456"  
       Height="256"/>

-4
只需在窗口上给出一个x:name,就可以检索其尺寸信息。
<Window x:Name="mainWindowContainer"
    x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="768" Width="1024" xmlns:my="http://schemas.sharpsoft.net
    [...]
</Window>

然后从代码中回忆实际窗口大小的信息。由于您已经有了托管大小和对象大小,所以只需进行数学计算。指定一个四面环绕背景的框架大小'frameWidth',图像的尺寸(destWidth,destHeight),您设置:
        int wOffset = (((int)mainWindowContainer.ActualWidth - frameWidth * 2) - destWidth) / 2 + frameWidth;
        int hOffset = (((int)mainWindowContainer.ActualHeight - frameWidth * 2) - destHeight) / 2 + frameWidth;

        Canvas.SetLeft(image, wOffset);
        Canvas.SetTop(image, hOffset); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接