c# - WPF LayoutTransform (Scale) throws Exception because DesiredSize of Image is NaN -
i setting imagesource of image this:
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;
the image uses scaletransform (layouttransform) in scrollviewer.
layouttransform needed update scrollviewers content size.
want scale image size of parent of scrollviewer (bounds):
double horizontalaspectratio = gridbounds.width / image.width; double verticalaspectratio = gridbounds.height / image.height; if (horizontalaspectratio > verticalaspectratio) { scaletransformimage.scalex = scaletransformimage.scaley = verticalaspectratio; messagebox.show("to height"); } else { scaletransformimage.scalex = scaletransformimage.scaley = horizontalaspectratio; messagebox.show("to width"); }
after doing invalidoperationexception thrown , says measuring image's layout requires desiresize not nan.
tried measure , arrange image manually this:
image.measure(new size(double.positiveinfinity, double.positiveinfinity)); image.arrange(new rect(0d, 0d, gridbounds.width, gridbounds.height));
but doesn't seem have effect.
starting out transforms , not have lot of knowledge yet....
as long did not explicitly set image control's width
, height
properties, values nan
, , aspect ratio calculation fail:
double horizontalaspectratio = gridbounds.width / image.width; double verticalaspectratio = gridbounds.height / image.height;
you may instead use image's actualwidth
, actualheight
, or if not yet laid out, width
, height
of source
:
double horizontalaspectratio = gridbounds.width / image.source.width; double verticalaspectratio = gridbounds.height / image.source.height;
Comments
Post a Comment