Resize image for use as an image brush

3.7k views Asked by At

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)

1

There are 1 answers

1
brunnerh On BEST ANSWER

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