Reputation: 2615
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
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
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