1

I want to add a Background colour and 9 Foreground Image to 9 buttons from code. I wish to change the images from C# not in WPF / xaml. The Background colour works OK using:

button1.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.Red);

Windows forms has an easy solution using:

pictureBox1.Image = Properties.Resources.P1; // this does not work for UWP

What I have tried so far ends up in error messages: I have changed the Build Action property of P1.png from Content to PRIResource with no success.

string url = "../../Images/P1.png";
//string url = "PW.png";
image1.Source = new BitmapImage(new Uri(url, UriKind.Relative)); //.Uri cannot be converted into a Windows.Foundation.Uri.
//image1.Source = new BitmapImage(new Uri(url, UriKind.Absolute)); //format of url could not be determined
<Button x:Name="button1" Content="Button"  Grid.Column="0" Grid.Row="0"  
                Tag="1" Background="Gray" Padding="0" UseLayoutRounding="False"  
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="button1_Click">
    <Button.Foreground>
        <ImageBrush Stretch="Fill" ImageSource="P1.PNG"/>
    </Button.Foreground>
</Button>
GolfCalcs
  • 21
  • 4
  • Have you tried to do in code the same you are doing in xaml? - create `new ImageBrush()`, set its *ImageSource* and then attach to your buttons? – Romasz Feb 10 '17 at 06:55
  • Possible duplicate of [wpf button with foreground and background as image](http://stackoverflow.com/questions/4107813/wpf-button-with-foreground-and-background-as-image) – M. Adeel Khalid Feb 10 '17 at 06:58
  • Hi Romasz. can you point me to some sample code on how to set the ImageSource and attach buttons. Thanks – GolfCalcs Feb 10 '17 at 07:15
  • Hi Romasz. Tried the following but received an error message: Uri cannot be converted into a Windows.Foundation.Uri ' ImageBrush imageBrush = new ImageBrush(); imageBrush.ImageSource = new BitmapImage(new Uri("Images/PW.PNG", UriKind.Relative)); button1.Foreground = imageBrush;' – GolfCalcs Feb 10 '17 at 07:29
  • Give it a try like this: `imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG"));` – Romasz Feb 10 '17 at 07:43
  • Hi Romasz. Great news! I am able to get a background image to work OK, thanks for that. ‘ ImageBrush imageBrush = new ImageBrush(); imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG")); button1.Background = imageBrush;’ but, I can’t get the foreground image to work. ‘button1.Foreground = imageBrush;’ Is there a different syntax for foreground. – GolfCalcs Feb 10 '17 at 12:20
  • There shouldn't be, have you tried to set foreground insead of backgorund? I'm not sure if imageBrush shouldn't be created anew each time. – Romasz Feb 10 '17 at 12:26
  • Hi Romasz No go. There is no error message, just no image visible. I also tried with imageBrush2. It only works with background, not foreground. ‘ImageBrush imageBrush2 = new ImageBrush(); imageBrush2.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG")); button2.Foreground = imageBrush2;’ BallintN – believes there is an error with Windows 8 and to “set it as backbround image (as opposed to foreground)” [link](http://stackoverflow.com/questions/24807740/button-image-not-visible ) – GolfCalcs Feb 10 '17 at 22:40

1 Answers1

0

Solution: To insert image to Button in C# using VS2015, UWP and Windows 8.1:

Add an Image to the front of your Button. eg Name = button1 and imageX. Ensure that you set “Build Action” Properties of *.png to Content.

enter code hereImageBrush imageBrush = new ImageBrush();
        imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG"));
        button1.Background = imageBrush;
 BitmapImage bitImage = new BitmapImage();
        bitImage.UriSource = new Uri("ms-appx:///Images/PW.PNG");
        imageX.Source = bitImage;

Thanks Romasz for your assistance.

GolfCalcs
  • 21
  • 4