-1

I want to show an image from Disk !

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    myImage.Source = new BitmapImage(
        new Uri("images\\Countries\\dz.png",UriKind.Relative));
}

I'm sure that the filename is correct but when I press the button, The image doesn't appear, I also made sure that the image is in the front of all other control and that myImage is its name .

Dan
  • 9,717
  • 4
  • 47
  • 65
Shika Taga Nai
  • 73
  • 1
  • 3
  • 11

4 Answers4

1

Try this:

private void Button_Click_3(object sender, RoutedEventArgs e)
{
    try
    {
        myImage.Source = new BitmapImage(
        new Uri(@"images\Countries\dz.png",UriKind.Relative));
    }

    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
  1. Putting @ makes it a literal string (you don't need to provide escape sequence then)

  2. If the above also doesn't work then it'll show the exception if any occurs.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Mujtaba Hassan
  • 2,495
  • 2
  • 20
  • 29
0

Try to Debug it. Put a break point on the line(F9) and hit F5 to start debugging. Check the value of myImage.Source after the line is executed (F10 to step). You can also use the Immediate Window to test other statements while debugging is paused and see the results.

Also make sure that the images folder is in the same folder as the executable file (in Debug or Release folders, according to your build)

Jerry
  • 4,258
  • 3
  • 31
  • 58
0
BitmapImage bitImg = new BitmapImage();
bitImg.BeginInit();
bitImg.UriSource = new Uri("images\\Countries\\dz.png", UriKind.Relative);
bitImg.EndInit();

myImage.Source = bitImg;
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
0

A few things that may help you:

Instead of:

myImage.Source = new BitmapImage(new Uri("images\\Countries\\dz.png",UriKind.Relative));

Try This:

BitmapImage ImageName = new BitmapImage;
ImageName.BeginInit();
ImageName.UriSource = new Uri(@"images\Countries\dz.png",UriKind.Relative);
ImageName.EndInit();
myImage.Source = ImageName;

See below link (examples section at the bottom of the page) for more info.

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx

If the above doesn't work, we will need you to post more code :). Also, it would help if you specify exactly what you are trying to achieve here.

Zak
  • 64
  • 8