Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF LayoutTransform(Scale)引发异常,因为图像的DesiredSize为NaN_C#_Wpf_Layout_Measure_Scaletransform - Fatal编程技术网

C# WPF LayoutTransform(Scale)引发异常,因为图像的DesiredSize为NaN

C# WPF LayoutTransform(Scale)引发异常,因为图像的DesiredSize为NaN,c#,wpf,layout,measure,scaletransform,C#,Wpf,Layout,Measure,Scaletransform,我正在设置图像的ImageSource,如下所示: Stream imageStreamSource = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read); TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource,BitmapCreateOptions.PreservePixelFormat, BitmapCacheOp

我正在设置图像的ImageSource,如下所示:

Stream imageStreamSource = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource,BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource bmSrc = decoder.Frames[0];
bmSrc.Freeze();
ImageSource = bmSrc;
image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
image.Arrange(new Rect(0d, 0d, gridBounds.Width, gridBounds.Height));
该图像在Scrollviewer中使用ScaleTransform(LayoutTransform)
需要LayoutTransform来更新ScrollViewer内容大小
我想将图像缩放到scrollviewer父级的大小(边界):

执行此操作后,将抛出InvalidOperationException,它表示测量图像的布局要求Desiredize不为NaN
我尝试手动测量和排列图像,如下所示:

Stream imageStreamSource = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource,BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource bmSrc = decoder.Frames[0];
bmSrc.Freeze();
ImageSource = bmSrc;
image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
image.Arrange(new Rect(0d, 0d, gridBounds.Width, gridBounds.Height));
但它似乎没有效果。

我刚开始学习变换,还没有太多的知识….

只要您没有明确设置图像控件的
宽度
高度
属性,它们的值将是
NaN
,纵横比计算将失败:

double horizontalAspectRatio = gridBounds.Width / image.Width;
double verticalAspectRatio = gridBounds.Height / image.Height;
您可以改为使用图像的
实际宽度
实际高度
,或者如果图像尚未布局,则使用其
源的
宽度
高度

double horizontalAspectRatio = gridBounds.Width / image.Source.Width;
double verticalAspectRatio = gridBounds.Height / image.Source.Height;

你把图像控件的宽度和高度设置在什么地方了吗?如果不是,则其值为NaN。您应该在纵横比计算中使用ActualWidth和ActualHeight。@克莱门斯现在似乎可以工作了,但实际值(用于比例计算)设置得不够早。那意味着我必须测量和安排,对吗?编辑:实际值为0d…当源属性已设置时,您可能还可以使用
image.Source.Width
image.Source.Height
@Clemens Works。想发布答案吗?