I am trying to load an image from an Embedded resource in a windows phone application
Here is my project setup:
Module1 = Phone Application
Module2 = ClassLibrary.dll
Module1 calls Module2 to create all of the data objects for the phone app.
When Module2 is creating the objects, I wanted to load an image from a resource.
The resource is "Default.png" and saved in the "Images" directory ( Build Action = Embedded Resource, Copy to Output Directory = Copy Always)
The code I am using produces an exception
ex = {"The request is not supported. "}
Here is the code:
private void LoadImage()
{
Assembly curAssembly = null;
curAssembly = Assembly.GetExecutingAssembly();
string [] names = curAssembly.GetManifestResourceNames();
// names[0] = "Storage.Images.Default.png"
// so I know I am using the correct name
Stream resStream = curAssembly.GetManifestResourceStream("Storage.Images.Default.png");
BitmapImage bitMapImage = new BitmapImage();
try
{
bitMapImage.SetSource(resStream);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
Can you help a newbie out? Thanks
Steps to get that working:
First Appraoch
Then then in your LoadImage method, do this:
private void LoadImage()
{
Drawing.Bitmap bitMapImage = resource_file.name_of_your_image;
}
Note: In this code, I assumed that the name you choose for your resource file is "resource_file"
Second Approach
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(this.GetType().Assembly.GetName().Name + ".resource_file", this.GetType().Assembly);
System.Drawing.Bitmap bmp= (System.Drawing.Bitmap)rm.GetObject("Baba");
or use can use System.Resources.ResourceReader and other approaches
Figured it out...
The name I was using to load the resource was wrong...
Using the name from the array returned in this call
string [] names = curAssembly.GetManifestResourceNames();
And it works flawlessly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.