in the following example, my border wraps around the image. The border does not wrap tightly around the image because I use DecodePixelWidth to keep the aspect ratio. Two sides end up with the border right up against the image, and the other two have gaps from the control. Is there a clean way to have the border wrap the image while keeping the aspect ratio instead of setting the Image stretch to fill.
BitmapImage bitmapIkon = new BitmapImage();
bitmapIkon.BeginInit();
bitmapIkon.CacheOption = BitmapCacheOption.OnLoad;
bitmapIkon.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmapIkon.UriSource = new Uri(imagePath);
bitmapIk开发者_开发知识库on.DecodePixelWidth = decodePixelWidth;
bitmapIkon.EndInit();
iImage.MinWidth=width;
iImage.MinHeight=height;
iImage.Source = bitmapIkon;
<Border Width="Auto" Height="Auto" Name="borderImageData" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" CornerRadius="0">
<Image Name="iImage" Stretch="Uniform" />
</Border>
Something like this should work:
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBox Name="iImage" Text="Uniform" Margin="1" />
<Border Name="borderImageData" BorderBrush="Black" BorderThickness="1" CornerRadius="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
So effectively, the Grid sizes to fit the Image plus a margin of 1. The Border then stretches to fill the Grid, and draws it's border on top of the image.
If you are going to be using this a lot, then you may want to wrap it in a custom control.
精彩评论