Click here to Skip to main content
15,846,727 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
17 Apr 2023CPOL1 min read 9.7K   9   9
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)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
BugEmbedded Twice Pin
Alan N23-Apr-23 13:12
Alan N23-Apr-23 13:12 
QuestionMemory Implications? Pin
skyscanner18-Apr-23 4:32
skyscanner18-Apr-23 4:32 
AnswerRe: Memory Implications? Pin
OriginalGriff18-Apr-23 5:25
mveOriginalGriff18-Apr-23 5:25 
QuestionBroken Images Pin
Graeme_Grant17-Apr-23 3:03
mvaGraeme_Grant17-Apr-23 3:03 
AnswerRe: Broken Images Pin
OriginalGriff17-Apr-23 3:24
mveOriginalGriff17-Apr-23 3:24 
GeneralRe: Broken Images Pin
Graeme_Grant17-Apr-23 3:34
mvaGraeme_Grant17-Apr-23 3:34 
GeneralRe: Broken Images Pin
OriginalGriff17-Apr-23 4:56
mveOriginalGriff17-Apr-23 4:56 
GeneralRe: Broken Images Pin
Graeme_Grant17-Apr-23 5:38
mvaGraeme_Grant17-Apr-23 5:38 
GeneralRe: Broken Images Pin
Graeme_Grant17-Apr-23 5:45
mvaGraeme_Grant17-Apr-23 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.