Set embedded image as button background - wpf

51 views Asked by At

I have to embed the all images (all images are stored under a folder 'images' on project) within the exe of wpf application created. For that I have set the property of image as below .

  • Build Action : Resource
  • Copy to output directory : Do not copy

Now I want to programmatically set the image as background of a button. I have done like below. But
shows exception

"System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'

        var brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("/images/btnbg.png", UriKind.Absolute));
        btn_show.Background = brush;

But I can set it correctly on xaml as below,

          <Button Margin="8" Width="160" Height="30"  BorderBrush="Transparent" Name="btn_show"  Style="{StaticResource CommonButton}" >
            <Button.Background>
                <ImageBrush ImageSource="/images/btnbg.png"/>
            </Button.Background>
            <TextBlock FontSize="14" FontFamily="Microsoft Sans Serif" >Show</TextBlock>
          </Button>
0

There are 0 answers