wpfxamlimagebrush

Resize image for use as an image brush


I want to

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)


Solution

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