How can I change image.source with C#?
Asked Answered
W

2

6

I'm developing a Windows Phone app.I have an image . This is its XAML code:

<Image x:Name="imageclock" Grid.Row="1" 
        Source="Image/Myimage.png" Height="240" Width="240"
        />

And i want change image.source with this code:

private void ClickonBtn(object sender, EventArgs e)
    {
            BitmapImage bm = new BitmapImage(new Uri("Image/Darktheme.png", UriKind.RelativeOrAbsolute));
            imageclock.Source = bm;
    }

But when i complied, imageclock.Source=Null and this is error:

An exception of type 'System.NullReferenceException' occurred in Newappver1.DLL but was not handled in user code

Wrestling answered 11/7, 2013 at 13:57 Comment(1)
And you verified that in the Image directory there is a file named Darktheme.png?Burrows
H
7

Your code looks ok but maybe you need to add @ before the image path to handle the / in the code behind like this :

BitmapImage bm = new BitmapImage(new Uri(@"Image/Darktheme.png", UriKind.RelativeOrAbsolute));
Hardiness answered 11/7, 2013 at 14:5 Comment(1)
You usually need to prepend a string with @ to preserve special characters (or you can escape them with backslash) but / is not one of them so you shouldn't need to escape it/use @Kreegar
M
0
imageclock.Source = new BitmapImage(new Uri("ms-appx:///Image/Darktheme.png"));

source https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.image.source.aspx

Marucci answered 26/9, 2015 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.