简体   繁体   中英

C# WPF Images not displaying

In my code, I have 3 images that are empty and then I set the source in the code behind. The relative path I'm giving them is the right one but the images are not displaying anyway. I got no clue how to fix this.

Xaml

<Border BorderThickness="1" Margin="44,135,433,248" BorderBrush="#FF000000">
  <Image x:Name="imageHelmet" HorizontalAlignment="Left" Height="116" Margin="-1" VerticalAlignment="Top" Width="127" MouseEnter="helmet_MouseEnter"/>
</Border>

Behind

string source = @"..\..\..\Images\" + piece.Link;
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(source, UriKind.Relative);
bmp.EndInit();
imageChestplate.Source = bmp;

This is just one of them, but the rest are the same.

The image is likely hidden due to the margin of the border, at least that's what I saw when I attempted to recreate your problem.

Here, the Margin Property of the Border is set to 44,135,433,248 . This means that there is a margin of 433 on the right side, and 248 on the bottom.

<Border BorderThickness="1" Margin="44,135,433,248" BorderBrush="#FF000000">

The edge of the window counts as something getting within the margin, so if the window is too small, it will not leave enough space for the margin, and as such the margin will be pushed over the image, hiding it.

To remedy this situation, change the right and bottom values in the margin to 0 , and set the Border 's HorizontalAlignment to Left , and VerticalAlignment to Top .

<Border BorderThickness="1" Margin="44,135,0,0" BorderBrush="#FF000000" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Image x:Name="imageHelmet" HorizontalAlignment="Left" Height="116" Margin="-1" VerticalAlignment="Top" Width="127" MouseEnter="helmet_MouseEnter"/>
</Border>

This way, there's no margin on the right and bottom sides to get pushed over the image, and the alignment makes it so the left and top alignments are used correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM