Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PictureBox not displaying the image

Tags:

c#

picturebox

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.

like image 996
Baub Avatar asked Feb 14 '14 17:02

Baub


2 Answers

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;
like image 172
Karam Najjar Avatar answered Oct 25 '22 05:10

Karam Najjar


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;
like image 35
Sudhakar Tillapudi Avatar answered Oct 25 '22 05:10

Sudhakar Tillapudi