UriKind Image Source

I have a project called "xx". I create a folder "images" that has this path: xx \ Bin \ Debug \ Images \

images contain only one photograph, this is the name "1.jpg", MainWindow contains Image control; I installed this code to download the image source, but it does not work why ??:

private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
    Image i = sender as Image; ;
    BitmapImage b = new BitmapImage(new Uri(@"images\1.jpg",UriKind.Relative));
    i.Source=b;
}

How to download the image source code by code? Thanks in advance.

+3
source share
2 answers

You need to add 1.jpgto your project in the folder images, and set Properties 1.jpg to Resource . To load a resource, use the packageURI conventions .

private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
    Image i = sender as Image; ;
    BitmapImage b = new BitmapImage(new Uri(@"pack://application:,,,/" 
         + Assembly.GetExecutingAssembly().GetName().Name 
         + ";component/" 
         + "Images/1.jpg", UriKind.Absolute)); 
    i.Source=b;
}
+2
source

try it

public void Image_MouseDown(object sender, RoutedEventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.InitialDirectory = "c:\\";
    dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
    dlg.RestoreDirectory = true;
    Nullable<bool> result = dlg.ShowDialog();                        

    if (result == true)
    {
        BitmapImage bitmap = new BitmapImage();
        Image img = sender as Image;
        bitmap.BeginInit();
        bitmap.UriSource = new Uri(dlg.FileName);
        bitmap.EndInit();
        img.Source = bitmap;
    }
}
0

All Articles