图像边缘行为

时间:2011-11-10 09:38:13

标签: c# wpf image margin

当我在运行时设置图像边距时,它会变得奇怪,与我通过xaml代码设置时不同。

我希望边距值为(40,16,0,0)。

当我在运行时添加它时,我的代码是这样的:

        System.Windows.Controls.Image proba = new System.Windows.Controls.Image();
        ImageSource imageSource = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");
        proba.Source = imageSource;
        proba.Width = 2245721 / 9525;
        proba.Height = 2286004 / 9525;
        proba.Margin = new Thickness(40, 16, 0, 0);
        gridName.Children.Add(proba);

但是,当我在xaml中添加图像时,cs中的代码就是:

        image1.Source = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");

,xaml中的代码是这样的:

<Window x:Class="GetInfoFromPptxFile.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="720" Width="960">

<Grid Name="gridName">
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <Image Height="150" HorizontalAlignment="Left" Margin="40,16,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>

当我运行程序时,image1正是我想要的位置,而名为proba的图像则不是。当我给他们相同的保证金值时,为什么会这样?

1 个答案:

答案 0 :(得分:4)

那是因为你没有在C#中设置水平和垂直对齐。

尝试添加:

proba.VerticalAlignment = VerticalAlignment.Top;
proba.HorizontalAlignment = HorizontalAlignment.Left;

它看起来也一样。