gpt4 book ai didi

c# - 拉伸(stretch)图像的宽度和高度

转载 作者:行者123 更新时间:2023-11-30 21:04:19 28 4
gpt4 key购买 nike

如果 ImageStretched,我可以获取 HeightWidth 吗>UniformToFill?

我尝试了 WidthHeight 属性,但它们始终是 NaN

最佳答案

如果想知道图片控件的尺寸,可以这样:

double theHeight = this.ActualHeight - img.Margin.Top - img.Margin.Bottom;
double theWidth = this.ActualWidth - img.Margin.Left - img.Margin.Right;

(img是上面代码中的图片控件名称,下面代码中)


如果您想知道图像的实际大小(在拉伸(stretch)之前),您可以试试这个:

BitmapSource SourceData = (BitmapSource)img.Source;
double imgWidth = SourceData.PixelWidth;
double imgHeight = SourceData.PixelHeight;

(我找到了这个 here )


此外,这还会在调整大小后(但在统一之前)为您提供图像尺寸:

double actWidth = img.ActualWidth;
double actHeight = img.ActualHeight;

因此,其中一个变量(actWidthactHeight)必须等于图像控制尺寸,而另一个将高于它。


请注意,如果您在 Window_Loaded 事件中调用第二个和第三个代码,它们将不起作用,因为此时还没有加载图像。您应该在加载所有内容后使用它。

关于c# - 拉伸(stretch)图像的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493004/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com