How to load embebbed resource image in WPF?

D.D.K-2637 966 Reputation points
2021-04-07T05:02:14.327+00:00

Hi,
from this post I was able to load the image with the absolute imagepath, but is this works on the client machine?

I found this answer quite similar to the way I do it on winform but I am having difficulty getting this Properties from code (WindowsFormsApplication1.Properties.Resources).

How to declare or access this Properties?

Thanks !

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-04-07T05:17:33.233+00:00

    There are articles like Accessing an Embedded Resource Using a Uri
    (with pack://application)


1 additional answer

Sort by: Most helpful
  1. HowardPaggerton 86 Reputation points
    2021-10-11T09:46:52.3+00:00

    To embed an image as a resource, add it to your project and set its Build Action to Resource. You can access this image at run-time using a Uri. The URI for a simple resource just includes the path to the image.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            BitmapImage bmi = new BitmapImage(new Uri("pack://application:,,,/Images/Ted.jpg"));
    
            MessageBox.Show(string.Format("Image is {0}x{1} pixels", bmi.Width, bmi.Height));
        }
        catch (Exception xx)
        {
            MessageBox.Show(xx.ToString());
        }
    }
    

    See details here

    3 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.