You need to use the UriKind.Absolute
for your Image path.
BitmapImage bmi =
new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png",
UriKind.Absolute));
bbb.Source = bmi ;
Question
Hi i have that kind of button:
XAML:
...
<Window.Resources>
<Style x:Key="NoChromeButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
<Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
...
<Button Name="aaa" Style="{DynamicResource NoChromeButton}" Margin="5" Height="30" Width="40" Click="Button_Click">
<StackPanel>
<Image Name="bbb" Source="C:\Users\Daniel\Desktop\WPF\Paint\skew.png"/>
</StackPanel>
</Button>
C#:
private void Button_Click(object sender, RoutedEventArgs e)
{
BitmapImage bmi = new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png", UriKind.Relative));
bbb.Source = bmi ;
}
When I click on button image just disappear, I tried several other methods for doing this but I can't do it. I search this forum but I didn't find anything that can really help me.
Solution
You need to use the UriKind.Absolute
for your Image path.
BitmapImage bmi =
new BitmapImage(new Uri("C:\\Users\\Daniel\\Desktop\\WPF\\Paint\\skew2.png",
UriKind.Absolute));
bbb.Source = bmi ;
OTHER TIPS
You haven't set the ContentPresenter
Content
property.
XAML
<Button x:Name="button" Content="Button1" HorizontalAlignment="Left" Margin="400,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.258,-5" Click="Button_Click" Height="80" Width="80"/>
C# (Set the original image in the properties of the button: right-click -> Brush -> image)
private void Button1_Click(object sender, RoutedEventArgs e)
{
button1.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("ms-appx:/Images/timerg.png", UriKind.RelativeOrAbsolute)) };
}
or C#
private void Button1_Click(object sender, RoutedEventArgs e)
{
BitmapImage bmp = new BitmapImage();
Uri u = new Uri("ms-appx:/Images/timer.png", UriKind.RelativeOrAbsolute);
bmp.UriSource = u;
// NOTE: change starts here
Image i = new Image();
i.Source = bmp;
button1.Content = i;
}