-1

I want to set width to dynamically added button background image.

this is my code

Buttob btn=new Button();
 ImageBrush brush1 = new ImageBrush();
 brush1.ImageSource = new BitmapImage(new Uri("ms-px:///Assets/emptyseat.jpg"));
   btn.Background = brush1;

how to set width of the above image dynamically.

1 Answers1

2

You can scale the image by creating a ScaleTransform object and applying it to the imageBrush, and setting the Stretch property on your brush to whatever it is you desire.

For example:

        Button btn = new Button();
        ImageBrush brush1 = new ImageBrush();            
        brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));

        ScaleTransform scaleTransform = new ScaleTransform();
        scaleTransform.ScaleX = 0.5;
        brush1.Transform = scaleTransform;
        brush1.Stretch = Stretch.Uniform;

        btn.Background = brush1;

It's not entirely clear what you are trying to achieve but the above will resize the image for you.