【问题标题】:WPF How to center the Image.SourceWPF 如何使 Image.Source 居中
【发布时间】:2011-03-11 15:36:20
【问题描述】:

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

在 WinForms 中,PicutreBox 控件具有 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图像。

【问题讨论】:

    标签: c# wpf image alignment center


    【解决方案1】:

    这是我的工作解决方案:

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

    【讨论】:

      【解决方案2】:

      只需在窗口上输入 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); 
      

      【讨论】:

        【解决方案3】:

        您应该尝试使用对齐方式将整个 Image 元素本身居中:

            <Grid>
                <Image Stretch="None"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
            </Grid>
        

        【讨论】:

        • 是的,我做到了。但由于某种原因,控件会呈现一种“缩略图”,而不是原始的。我认为这是因为我加载资源的方式,即不使用 URI。
        【解决方案4】:

        Stretch 设置为无。

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

        【讨论】:

        • 我试过了,但没有达到我期望的效果。到目前为止,它大大缩小了我的图像并将其放在左上角。无:img28.imageshack.us/img28/1783/imagestretchnone.png 统一:img810.imageshack.us/img810/2401/imagestretchuniform.png
        • @unexpectedkas:您能否编辑您的问题以包含您的一些 XAML?默认情况下,图像将在 Image 控件中居中绘制,但 Image 控件可能不会居中或跨其父控件拉伸。例如,如果您的整个窗口是一个带有一个图像的网格,那么它将居中,但如果它是一个带有一个图像的画布,那么它将位于左上角。
        • 好的,我刚刚做了。我将 H/V align 设置为居中,是的,图像居中。非常感谢。无论如何,要正确拉伸必须是“统一”。我很困惑,因为控件在窗口上移动了 ¬¬'
        猜你喜欢
        • 2011-02-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多