Question

I want to

  • load an image from a resource
  • Set the image width, ensuring that it scales it's height proportionally.
  • Use it as a background brush

It can't figure out the "setting its width" part.

The compromise I'm currently using is to scale it to half size. This isn't what I ideally want to do, I want to set the width to an absolute value, regardless of what the original image size was.

<BitmapImage x:Key="floorPlan"
        UriSource="/NavigationPathEditor;component/Images/CairnsFloorPlansCropped.png"/>

<TransformedBitmap x:Key="resizedFloorPlan" Source="{StaticResource floorPlan}">
    <TransformedBitmap.Transform>
        <ScaleTransform 
                CenterX="0"   CenterY="0" 
                ScaleX="0.5"  ScaleY="0.5" />
    </TransformedBitmap.Transform>
</TransformedBitmap>

<ImageBrush 
      x:Key="floorPlanBrush" 
      TileMode="None" Stretch="None" 
      AlignmentX="Left" AlignmentY="Top"
      ImageSource="{StaticResource resizedFloorPlan}" />

(Incidentally, the above code works at runtime, but throws an error in the designer)

Was it helpful?

Solution

The easiest way to do that would probably be setting the DecodePixelWidth/Height on the image itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top