简体   繁体   English

拉伸图像以填充可用的宽度/高度(单独)

[英]Stretch image to fill available width / height (separately)

Is it any way to fill available width / height with image in xaml? 是否可以用xaml中的图像填充可用的宽度/高度? I need something like UniformToFill, but where I can control stretch direction (width or height) 我需要像UniformToFill这样的东西,但我可以控制拉伸方向(宽度或高度)

Assume I have have following code: 假设我有以下代码:

<UniformGrid Columns="2" Rows="2">        
    <Image Source="Desert1.jpg" Stretch="Uniform"/> //
    <Image Source="Desert2.jpg" Stretch="UniformToFill"/> // 
    <Image Source="Desert3.jpg" />
    <Image Source="Desert4.jpg" />
</UniformGrid>

EDIT: for examle (width): if image is half as wide as I want to show, I don't care about height and just scale x2 image height and width. 编辑:对于考试(宽度):如果图像是我想要显示的一半宽,我不关心高度,只是缩放x2图像的高度和宽度。 So image must fit width, and don't care about height. 因此图像必须适合宽度,而不关心高度。 It's desired behaviour, but if it's not possible - ok. 这是理想的行为,但如果不可能 - 好吧。 So you can rethink question as IF it possible, HOW can I do it in xaml. 所以你可以重新考虑问题,如果可能的话,我怎样才能在xaml中完成。

Also all images may have different width and height 此外,所有图像可能具有不同的宽度和高度

I think that you might be able to get the effect you desire in certain conditions. 我认为你可以在某些条件下获得你想要的效果。 If your images are all bigger than the size that they will be displayed, you could possibly use this: 如果您的图像都大于它们将显示的大小,您可以使用此:

<Image Source="Desert.jpg" Stretch="UniformToFill" StretchDirection="DownOnly" />

A ViewBox has the same Stretch properties as an Image and there is a good example of the differences between the different combinations in the How to: Apply Stretch Properties to the Contents of a Viewbox article on MSDN. 一个ViewBox具有相同的Stretch性能的Image并没有在不同组合之间的差异一个很好的例子如何:应用拉伸性能,以视框的内容 MSDN上的文章。

This might be what you are looking for... 这可能就是你要找的......

TransformedBitmap

Here is a static method I made in an ImageUtility class. 这是我在ImageUtility类中创建的静态方法。

public static TransformedBitmap GetScaledBitmapImageSprite(BitmapSource src, double x_scale, double y_scale)
{
  return (new TransformedBitmap(src, new ScaleTransform(x_scale, y_scale)));
{

The x_scale and y_scale are doubles in the form of: x_scale和y_scale是以下形式的双打:

desired_width / original_width desired_width / original_width

Maybe a little different than what you are looking for but I think it can get you started on the right track. 也许与你想要的有点不同,但我认为它可以让你开始走上正轨。

You can store your TransformedBitmap in memory and apply new transforms through: 您可以将TransformedBitmap存储在内存中并通过以下方式应用新的转换:

TransformedBitmap x = new TransformedBitmap();
x.Transform = new ScaleTransform(x,y);

You should use 你应该用

<Image Source="{Binding Image}" Stretch="Fill"/>

like if you use Stretch="UnifrmtoFill" then it will change both length and width in a ratio or I say both together. 就像你使用Stretch =“UnifrmtoFill”一样,它会以一定比例改变长度和宽度,或者我同时说两者。

so if you use Stretch="Fill", it gives you chance to change either height or width at a time whatever is changed. 因此,如果您使用Stretch =“Fill”,它可以让您随时更改任何高度或宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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