caesay
caesay

Reputation: 17233

accessing pictures in my exe of wpf app from codebehind

im currently accessing my image in XAML and setting it to a picturebox with

Source="/SocialShock-WPF-Client;component/Images/blue-bar-replication.png"

how would i do this in the codebehind?

Upvotes: 0

Views: 671

Answers (2)

Anvaka
Anvaka

Reputation: 15823

Exactly the same way. Create BitmapImage object (pass Uri object with the same path to its constructor). Then set Image's Source property to the newly created BitmapImage object.

Upvotes: 0

serine
serine

Reputation: 1380

    public static BitmapImage GetImageFromResource(string name)
    {
        var res = new BitmapImage();
        res.BeginInit();
        res.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream("SocialShock-WPF-Client.Images." + name);
        res.EndInit();

        return res;
    }

Invoke this method with the image name, "blue-bar-replication.png" in your case. The image build action should be set to EmbededResource.

Upvotes: 1

Related Questions