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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18250340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:26:50  来源:igfitidea点击:

Stretch image to fill available width / height (separately)

c#wpfimage

提问by Eugene

Is it any way to fill available width / height with image in xaml? I need something like UniformToFill, but where I can control stretch direction (width or height)

有没有办法用xaml中的图像填充可用宽度/高度?我需要像 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. 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.

编辑:例如(宽度):如果图像是我想要显示的宽度的一半,我不关心高度,只是缩放 x2 图像的高度和宽度。所以图像必须适合宽度,而不关心高度。这是理想的行为,但如果不可能 - 好的。所以你可以重新思考问题,如果可能的话,我怎么能在 xaml 中做到这一点。

Also all images may have different width and height

此外,所有图像可能具有不同的宽度和高度

回答by Sheridan

I think that you mightbe 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 couldpossibly use this:

我认为在某些条件下您可能会获得您想要的效果。如果您的图像都大于它们将显示的尺寸,您可以使用以下方法:

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

A ViewBoxhas the same Stretchproperties as an Imageand there is a good example of the differences between the different combinations in the How to: Apply Stretch Properties to the Contents of a Viewboxarticle on MSDN.

AViewBox具有与Stretchan相同的属性,Image并且在 MSDN 上的How to: Apply Stretch Properties to the Contents of a Viewbox文章中有一个很好的示例来说明不同组合之间的差异。

回答by mwjohnson

This might be what you are looking for...

这可能是您正在寻找的...

TransformedBitmap

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

required_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);

回答by DeshDeep Singh

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",则无论发生什么变化,您都可以一次更改高度或宽度。