简体   繁体   中英

C# / Visual Studio: How to include images but without embedding them into the assembly?

I am developing a piece of software that uses icons for various menus. I want the user to be able to change them at will by just replacing their files in a subdirectory that is right next to my assembly. Like so:

Program.exe
Data/Images/new.png
Data/Images/save.png
Data/Sounds/reload.wav

How can I achieve this kind of structure and still see the icons in the WinForms designer?

How can I integrate a structure like this reliably into my Solution?

I was unable to find any info on this online since it seems to be normal to include them in the assembly instead.

you can right click on your project file and choose Add -> New Folder . After that, right click on your newly created folder and select Add -> Existing Item... . Browse your disk and add your image(s). Right click on your image(s) and select Properties. In properties windows, under Copy To Output Directory choose Copy if newer option.

That way images will be included in project, but not embedded into .exe

edit - few examples: to use .ico file as your form icon, you can write something like this:

string path = Path.Combine(Application.StartupPath, @"Data\Images\new.ico");
this.Icon = new Icon(path);

or, to load image and put it into PictureBox:

string path = Path.Combine(Application.StartupPath, @"Data\Images\new.jpg");
this.pictureBox1.Image = Image.FromFile(path);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM