0

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)

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205

1 Answers1

1

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

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thanks for your answer. While you were writing your answer, I changed the question a little because I figured out how to use the TransformedBitmap element. I've made the problem more specific now: I want to set the image to an absolute width rather than a proportional width. – Andrew Shepherd Feb 02 '12 at 23:04