Properties.Resources not loading an image

前端 未结 2 802
悲哀的现实
悲哀的现实 2021-01-15 09:57

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 a

2条回答
  •  伪装坚强ぢ
    2021-01-15 10:33

    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.

提交回复
热议问题