6

In my C# project I have used the Tools to add a PictureBox control. I have set the image location to a location of a PNG on our network and I am having the same trouble as others where if the images are moved from that directory then the image in my program will not display.

I'd like to embed the images into the EXE when it compiles so the images aren't dependent on the actual file being available on the network. I have found an article that says to make my image a resource in my project. I did that but when I try to use it, I see the error: stream is not a valid resource file.

I know this can be accomplished but I, so far, haven't found what I need to do.

How should I set this up? Because I obviously have the wrong setup now.

Baub
  • 723
  • 4
  • 21
  • 36
  • 2
    can you post your code? – Sudhakar Tillapudi Feb 14 '14 at 17:52
  • I don't have code for the picturebox. I set everything for that in the Properties pages. – Baub Feb 14 '14 at 17:58
  • 1
    you said -> `but when I try to use it, I see the error: stream is not a valid resource file.` what does `I try to use it` means here? – Sudhakar Tillapudi Feb 14 '14 at 18:00
  • when I add my image as a resource, I then go to the property pages of the picturebox and I remove the path from the ImageLocation and then try to set the InitialImage. In the 'Select Resource' box that appears, I see the resource name but can't select it because I get the message: Stream is not a valid resource file. – Baub Feb 14 '14 at 18:17
  • Don't set `InitialImage`, use the following; `pictureBox.Image = Properties.Resources.YourEmbeddedImageName` – txtechhelp Feb 14 '14 at 18:19
  • I get error "Properties is not declared" ... – Prescott Chartier May 10 '18 at 14:42

3 Answers3

17

Step1: RightClick on the Project

Step2: Select Properties.

Step3: Go to Resources tab.

Step4: Click on Add Resource Dropdown menu shown in below pic.

Step5: Select Add Existing File...

Step6: now select the file which you want to add as Resource from File Browse Dialog.

Step7: select the picture that is added and set the Persistence property to Embedded in .resx

then the picture is embedded with your application ;)

to access it

pictureBox1.Image = Properties.Resources.ImageName;
Karam Najjar
  • 527
  • 5
  • 21
  • tried it but no dice. still, the PictureBox will not populate even though i have the pix in the Resources folder and set the Persistence as you recommended, and the pix even displays fine in the Designer. makes no sense. i'm fast becoming a raving lunatic. – pdp Apr 10 '21 at 08:49
  • If it still doesn't work after doing this, make sure to clear the ImageLocation property below "Image") in the properties window of the PictureBox. – Lunster Dec 03 '21 at 17:45
3

Solution 1: you can use PictureBox Control to display the images on Windows Form.

Follow the below steps:

drag and drop the PictureBox control from controls ToolBox to WindowsForm.

now in button Click event handler write the following code:

private void Button1_Click(object sender, EventArgs e)
    {          
        pictureBox1.Image = Image.FromFile("path of imge file");
    }

Solution 2 : if you want to access the images from project executable you need to Add those image files as Resources to your project.

follow the below steps:

Step1: RightClick on the Project

Step2: Select Properties.

Step3: Goto Resources tab as shown in below pic.

Step4: Click on Add Resource Dropdown menu shown in below pic.

Step5: Select Add Existing File...

Step6: now select the file which you want to add as Resource from File Browse Dialog.

Sample ScreenShot:

enter image description here

And try to access the file from the Code as below:
Note : I have added resource with name : sudhakar.

pictureBox1.Image = Properties.Resources.sudhakar;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
-1

You can add your icon to resources of your application.

  1. Open Resources window from Properties/Resources in Solution explorer. enter image description here
  2. Choose "Add resource" -> Add Existing file. enter image description here
  3. Choose your icon to add and save changes in Resources.
  4. Now you can use this icon in PictureBox properties dialog for selecting icon enter image description here
Nick
  • 798
  • 1
  • 7
  • 14