2011-04-18 86 views
4

在以下示例中,我的边框环绕图像。因为我使用DecodePixelWidth来保持宽高比,所以边框不会紧紧包裹图像。双方的边界正好对着形象,而另外两个则与控制有所不同。有没有一种干净的方式来让边框包裹图像,同时保持纵横比而不是设置图像拉伸填充。图像边框控制包裹问题

BitmapImage bitmapIkon = new BitmapImage(); 
bitmapIkon.BeginInit(); 
bitmapIkon.CacheOption = BitmapCacheOption.OnLoad; 
bitmapIkon.CreateOptions = BitmapCreateOptions.IgnoreImageCache; 

bitmapIkon.UriSource = new Uri(imagePath); 

bitmapIkon.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> 

回答

1

像这样的东西应该工作:

<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> 

所以有效的网格大小以适应图像加1.保证金然后两国边境长达填补了电网,并在图像上绘制边框。

如果你打算使用这个很多,那么你可能想要把它包装在一个自定义控件中。

+0

谢谢,这让我走上了正轨。我在用户控件中有这个。我不得不改变它一点,可能也影响了一些主窗口网格。这是我最终的结果,只是略有不同。 Terco 2011-04-19 01:35:23