none
Set Image Source in code?

    Question

  • I have a image which of course has a "Source" property that allows me to specify an image .  In XAML, I can successfully do this via:

     

    <Image Source="/MyAssembly;Component/Images/myImage.png" ......

     

    How do I do this in C#?

     

     

    Thursday, September 20, 2007 1:58 PM

Answers

  • string ImagesPath = "pack://application:,,/MyAssembly;component/Images/myImage.png";
    Uri uri = new Uri(ImagesPath, UriKind.RelativeOrAbsolute);
    BitmapImage bitmap = new BitmapImage(uri);
    Image img = new Image();
    img.Source = bitmap;


    For cursors file (*.cur) on another:

    string ImagesPath = "pack://application:,,/MyAssembly;component/Images/myImage.png";
    Uri uri = new Uri(ImagesPath, UriKind.RelativeOrAbsolute);
    Stream stream = Application.GetResourceStream(uri).Stream;
    Cursor cursor = new Cursor(stream);
    this.Cursor = cursor;
    Thursday, September 20, 2007 2:26 PM
  • Thursday, September 20, 2007 2:18 PM
    Moderator

All replies

  • Thursday, September 20, 2007 2:18 PM
    Moderator
  • string ImagesPath = "pack://application:,,/MyAssembly;component/Images/myImage.png";
    Uri uri = new Uri(ImagesPath, UriKind.RelativeOrAbsolute);
    BitmapImage bitmap = new BitmapImage(uri);
    Image img = new Image();
    img.Source = bitmap;


    For cursors file (*.cur) on another:

    string ImagesPath = "pack://application:,,/MyAssembly;component/Images/myImage.png";
    Uri uri = new Uri(ImagesPath, UriKind.RelativeOrAbsolute);
    Stream stream = Application.GetResourceStream(uri).Stream;
    Cursor cursor = new Cursor(stream);
    this.Cursor = cursor;
    Thursday, September 20, 2007 2:26 PM
  • I'm trying to do something relatively similar... I've got a UserControl that I'm trying to create where I can assign an Icon to it like so:

    <m:NavButton Icon="assets/icons/whatever.png" />

    To do this, I've created a DependencyProperty on Icon and that part is work well. It's the part of adding the image I'm having trouble with.  Right now it can't seem to find the Image.  What I've got right now is this:

     public static void OnIconChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
            {
                NavButton item = (NavButton)obj;
                BitmapImage bitImg = new BitmapImage();
                bitImg.BeginInit();
                bitImg.UriSource = new Uri(item.Icon, UriKind.Relative);
                bitImg.DecodePixelWidth = 16;
                bitImg.EndInit();
                item.iconImage.Source = bitImg;
            }

    This doesn't throw any exceptions, but it also doesn't show the icon. Just blank space.  I'd also be curious to know what exactly the "pack://application:,," does (especially the commas) since I'm kind of lost on that. I did try adding that to item.Icon and changing UriKind to RelativeOrAbsolute -- but instead I just got a message that it couldn't find the resource.
    Thursday, September 20, 2007 3:54 PM
  • string ImagesPath = "pack://application:,,/MyAssembly;component/Images/myImage.png";
    Uri uri = new Uri(ImagesPath, UriKind.RelativeOrAbsolute);
    BitmapImage bitmap = new BitmapImage(uri);
    Image img = new Image();
    img.Source = bitmap;


    For cursors file (*.cur) on another:

    string ImagesPath = "pack://application:,,/MyAssembly;component/Images/myImage.png";
    Uri uri = new Uri(ImagesPath, UriKind.RelativeOrAbsolute);
    Stream stream = Application.GetResourceStream(uri).Stream;
    Cursor cursor = new Cursor(stream);
    this.Cursor = cursor;
    Thank you for the post and because my wpf form and the picture in the same project I just the name of my picture in ImagePath and everything worked normally 

    Omar Adnan Isaid http://omarpost.blogspot.com/


    Sunday, April 15, 2012 9:58 AM