Bernice
Bernice

Reputation: 2592

Properties.Resources not loading an image

I have a game application in Visual Studio 2012 C#. I have all the .png images I am using in the Resources file of the project.

Have you any idea why I can access all the files but one by using Properties.Resources?

I checked the full filePath and it's set to the resources folder. And it's added in the program as I did Add -> Existing Item and added the image.

It looks just like the other images. I have no idea why it's not loading. I need this since I need to send a .exe by email to my lecturer and without this image the project is nothing!

enter image description here

I added this in the resource file

internal static System.Drawing.Bitmap grid_fw {
            get
            {
                object obj = ResourceManager.GetObject("grid.fw", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }

and although now grid is available, it is returning null :/

Upvotes: 4

Views: 23683

Answers (2)

justderb
justderb

Reputation: 2854

Found from: Properties.Resources the icon name does not appear in the intellisense

You also need to add the icon to the Resources.resx file. Open it in Visual Studio and drag your icon into the Icons menu of the resx and it will become available.

Also, see Adding and Editing Resources (Visual C#)

Upvotes: 5

MethodMan
MethodMan

Reputation: 18843

You can get a reference to the image the following way:

Image myImage = Resources.yourImage;

If you want to make a copy of the image, you'll need to do the following:

Bitmap bmp = new Bitmap(Resources.yourImage);

Don't forget to dispose of bmp when you're done with it. If you don't know the name of the resource image at compile-time, you can use a resource manager:

ResourceManager rm = Resources.ResourceManager;
Bitmap yourImage = (Bitmap)rm.GetObject("yourImage");

The benefit of the ResourceManager is that you can use it where Resources.myImage would normally be out of scope, or where you want to dynamically access resources. Additionally, this works for sounds, config files, etc.

Upvotes: 1

Related Questions