Problem
In my project, I have an image called Resources/myimage.jpg. How do I load this image into a Bitmap object dynamically?
Asked by Pavel Bastov
Solution #1
Do you make use of Windows Forms? If you used the Properties/Resources UI to upload the picture, you’ll have access to it via produced code, so you can easily do this:
var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage);
Answered by Matt Hamilton
Solution #2
The image can be referenced in the following way:
Image myImage = Resources.myImage;
You’ll need to do the following if you want to create a copy of the image:
Bitmap bmp = new Bitmap(Resources.myImage);
When you’re finished with bmp, don’t forget to delete it. You can use a resource manager if you don’t know the name of the resource image at compile time:
ResourceManager rm = Resources.ResourceManager;
Bitmap myImage = (Bitmap)rm.GetObject("myImage");
The ResourceManager’s advantage is that it can be used in situations where Resources.myImage would ordinarily be out of scope or if you want to access resources dynamically. This also applies to sounds, config files, and other files.
Answered by Charlie Salts
Solution #3
It has to be loaded from the resource stream.
Bitmap bmp = new Bitmap(
System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.myimage.png"));
If you want to know the names of all the resources in your assembly, type:
string[] all = System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceNames();
foreach (string one in all) {
MessageBox.Show(one);
}
Answered by Josip Medved
Solution #4
Most of the recommended answers are far more difficult.
tslMode.Image = global::ProjectName.Properties.Resources.ImageName;
Answered by JDS
Solution #5
The best option is to add them as Image Resources in the Project’s Resources settings. The image can then be obtained directly using Resources.myimage. The image will be obtained using a produced C# property.
You can get it with: if you only put the image as an Embedded Resource.
string name = "Resources.myimage.jpg"
string namespaceName = "MyCompany.MyNamespace";
string resource = namespaceName + "." + name;
Type type = typeof(MyCompany.MyNamespace.MyTypeFromSameAssemblyAsResource);
Bitmap image = new Bitmap(type.Assembly.GetManifestResourceStream(resource));
Where MyTypeFromSameAssemblyAsResource is any type from the same assembly as the resource.
Answered by Hallgrim
Post is based on https://stackoverflow.com/questions/1192054/load-image-from-resources-area-of-project-in-c-sharp