gpuguy
gpuguy

Reputation: 4585

No Image display in c#

I created a simple windows form application C#, that is supposed to show a picture. I am following a tutorial from here Following is the code of form1.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        // Construct an image object from a file in the local directory.
        // ... This file must exist in the solution.
        Image image = Image.FromFile("Picture1.png");
        // Set the PictureBox image property to this image.
        // ... Then, adjust its height and width properties.
        pictureBox1.Image = image;
        pictureBox1.Height = image.Height;
        pictureBox1.Width = image.Width;
    }
}

The image is present in the solution folder, but still nothing is displayed in the window when the application is run. There is no compilation error.

Update

enter image description here

Its solved now.

enter image description here

Upvotes: 1

Views: 1156

Answers (3)

Victor Zakharov
Victor Zakharov

Reputation: 26454

A better option would be to add this image to the project and do either of the two:

  • Add as a resource. It can later be used with strong typing.
  • Set this option on the file: Copy to Output Directory = Copy if newer.

It is not a good practice to add anything manually under /bin/, because those folders are volatile and can be erased by Visual Studio at any moment (usually on the next rebuild).

Upvotes: 0

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112712

Make sure the PictureBox click event handler is registrated. It is not sufficient to copy the example code. (I'm just guessing)

enter image description here

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65087

Without a full path, the image won't load from the solution directory.. it will load from the directory where your executable is executed from.. which is either the Debug or Release folders when you're running from Visual Studio.. depending on what Profile you're running it with.

So put the image in the /bin/Debug folder and run your program again.

Upvotes: 1

Related Questions