1

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>
user2431727
  • 877
  • 2
  • 15
  • 46
  • 2
    In code, use a full Resource File Pack URI: `new Uri("pack://application:,,,/images/btnbg.png")` – Clemens Apr 27 '20 at 09:34

0 Answers0