Load image from resources area of project in C#

2018-12-31 19:58发布

I have an image in my project stored at Resources/myimage.jpg. How can I dynamically load this image into Bitmap object?

16条回答
倾城一夜雪
2楼-- · 2018-12-31 20:24

With and ImageBox named "ImagePreview FormStrings.MyImageNames contains a regular get/set string cast method, which are linked to a scrollbox type list. The images have the same names as the linked names on the list, except for the .bmp endings. All bitmaps are dragged into the resources.resx

Object rm = Properties.Resources.ResourceManager.GetObject(FormStrings.MyImageNames);
Bitmap myImage = (Bitmap)rm;
ImagePreview.Image = myImage;
查看更多
大哥的爱人
3楼-- · 2018-12-31 20:24

JDS's answer worked best. C# example loading image:

  • Include the image as Resource (Project tree->Resources, right click to add the desirable file ImageName.png)
  • Embedded Resource (Project tree->Resources->ImageName.png, right click select properties)
  • .png file format (.bmp .jpg should also be OK)

pictureBox1.Image = ProjectName.Properties.Resources.ImageName;

Note the followings:

  • The resource image file is "ImageName.png", file extension should be omitted.
  • ProjectName may perhaps be more adequately understood as "Assembly name", which is to be the respective text entry on the Project->Properties page.

The example code line is run successfully using VisualStudio 2015 Community.

查看更多
只靠听说
4楼-- · 2018-12-31 20:26

Code I use in several of my projects... It assumes that you store images in resource only as bitmaps not icons

    public static Bitmap GetImageByName(string imageName)
    {
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        string resourceName = asm.GetName().Name + ".Properties.Resources";
        var rm = new System.Resources.ResourceManager(resourceName, asm);
        return (Bitmap)rm.GetObject(imageName);

    }
查看更多
笑指拈花
5楼-- · 2018-12-31 20:27

Way easier than most all of the proposed answers

tslMode.Image = global::ProjectName.Properties.Resources.ImageName;
查看更多
临风纵饮
6楼-- · 2018-12-31 20:28

You can also save the bmp in a var like this:

var bmp = Resources.ImageName;

hope it helps!

查看更多
人间绝色
7楼-- · 2018-12-31 20:29

In my case -- I was using Icons in my resource, but I needed to add them dynamically as Images to some ToolStripMenuItem(s). So in the method that I created (which is where the code snippet below comes from), I had to convert the icon resources to bitmaps before I could return them for addition to my MenuItem.

string imageName = myImageNameStr;
imageName = imageName.Replace(" ", "_");
Icon myIcon = (Icon)Resources.ResourceManager.GetObject(imageName);
return myIcon.ToBitmap();

Something else to be aware of, if your image/icon has spaces (" ") in its name when you add them to your resource, VS will automatically replace those spaces with "_"(s). Because, spaces are not a valid character when naming your resource. Which is why I'm using the Replace() method in my referenced code. You can likely just ignore that line.

查看更多
登录 后发表回答