I have subclassed the image control (to display images at their true pixel size, regardless of resolution setting in metadata) and I want to implement scrolling and zooming, by using a ScaleTransform (within a LayoutTransform). This works fine at 100%, but when scaled the scrolling size stays at the size of the 100% image.
My XAML is:
<ScrollViewer Name="imgScrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<PL:DpiAgnosticImage x:Name="Ctrl_ImgMain" Stretch="None" HorizontalAlignment="Left" MouseWheel="Ctrl_ImgMain_MouseWheel" VerticalAlignment="Top" >
<Image.LayoutTransform>
<ScaleTransform ScaleX="{Binding ZoomLevel}" ScaleY="{Binding ZoomLevel}">
</ScaleTransform>
</Image.LayoutTransform>
</PL:DpiAgnosticImage>
</ScrollViewer>
The C# for my DpiAgnosticImage class is:
class DpiAgnosticImage : Image
{
protected override Size MeasureOverride(Size constraint)
{
var bitmapImage = Source as BitmapImage;
var desiredSize = bitmapImage == null
? base.MeasureOverride(constraint)
: new Size(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
return desiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
return new Size(Math.Round(DesiredSize.Width), Math.Round(DesiredSize.Height));
}
}
I’ve searched around but the closest that I have found to this problem suggests using LayoutTransform – which I already am.
I suspect that I need to implement something to change the scrollviewer size when the image is scaled, but I am struggling to find out what is required. Any suggestions would be appreciated.
Your derived image control works fine if you simply don’t override
ArrangeOverride.Apparently the
DesiredSizeproperty gets the size calculated byMeasureOverrideand subsequently transformed byLayoutTransform. This might by obvious during layout, although it doesn’t seem to be well documented in the MSDN of DesiredSize and MeasureOverride.Found this in Charles Petzold, “Applications=Code+Markup”, p.831:
Anyway, if you need to override
ArrangeOverridefor any other reason, simply return the value of thefinalSizeargument.