taji01
taji01

Reputation: 2615

Open only specific image to picturebox without opening File Dialog

I'm using WinForms. In my form i have a picturebox, and a button. I'm trying to load a picture in my picturebox on button click from a file inside my computer. I don't want to open a file dialog-box. Every time i try to to open the image, i receive an error.

Error Message: An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dl

There is only one picture which has a .png extension in my image folder. Is there a way for me to just open .png picture in my picturebox without specifying the file name? I think i might be able to do this with just specifying the file extension. In this case something like C\image.png. How would i do this?

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(@"‪‪C:\\image\\resized_tree.jpg");
    }

Upvotes: 2

Views: 2564

Answers (2)

Chris
Chris

Reputation: 639

As you mentioned above you cannot include your images to your existing application. You can load your images thru FIleStream.

Below Sample

   FileStream fs = new FileStream(@"‪‪C:\\image\\resized_tree.jpg", FileMode.Open, FileAccess.Read);
   picturebox1.Image = Image.FromStream(fs);

Upvotes: 2

jsanalytics
jsanalytics

Reputation: 13188

Try this:

    private void button1_Click(object sender, EventArgs e)
    {
        string path = @".\";
        string[] filename = Directory.GetFiles(path, "*.png");

        pictureBox1.Load(filename[0]);
    }

Upvotes: 3

Related Questions