Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Adding Images to a Winforms Project so They are Accessible as Resources

5.00/5 (5 votes)
17 Apr 2023CPOL1 min read 17.8K  
How to add images to a Winforms Project so they are accessible as resources
Buttons with images on are sometimes a lot easier to use than text based buttons - and can take up less space where it gets critical. But ... it can be confusing to get them into the app so they are available as Image objects directly, compiled into your app. So I thought I'd write it down for next time I have to do it so I - and maybe you - have an easier time.

Introduction

Buttons with images on are sometimes a lot easier to use than text based buttons - and can take up less space where it gets critical.

But ... it can be confusing to get them into the app so they are available as Image objects directly, compiled into your app.

So I thought I'd write it down for next time I have to do it so I - and maybe you - have an easier time.

Background

First, create a folder to put them in: right click your project and select "Add ... New Folder". Rename it to "Resources".

Image 1

Right click the "Resources" folder, and select "Add ... New Folder" - rename this to "Images"

Image 2

Right click the Images folder and select "Add ... Existing Item". Browse to the images (you may need to change the "type" dropdown) and select all those you are interested in. Add them to the project.

Image 3

In the Images folder, highlight all the images you just added, and look at the properties pane. The "Build Action" will be set to "Content" - change it to "Embedded Resource".

Image 4

Open the "Resources.resx" file of your project.

Image 5

Select "Images" at the top left. Drag and drop the Images folder onto the .resx window.

Image 6

Open your code, and you can get the Image object like this:

C#
Image i = Properties.Resources.NameOfImageWithoutExtension;

Or set it as the Image property for a Button or PictureBox for example.

Image 7

Image 8

History

  • 17th April, 2023: First version
  • 17th April, 2023: A few minutes later ... Why did half the images disappear? Re-inserted
  • 17th April 2023: Images re-uploaded, and re-re-inserted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)