Click here to Skip to main content
15,668,167 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I currently have a function to allow people to open solely pictures in my C# Windows form application.
How do I display the opened picture into a picture box? (So people can view the image they just opened in the form application)

Thank you for your answers.

What I have tried:

I have looked around this forum for several minutes to no avail.
Updated 29-Aug-22 5:52am

Share this answer
You might want to read up on the documentation for the PictureBox Class (System.Windows.Forms) | Microsoft Docs[^]
Share this answer
There are problems with Images which aren't immediately obvious: for example, when you load an image from a file using the Image.FromFile Method (System.Drawing) | Microsoft Docs[^] it creates a Stream object to read the file, and creates the bitmap from that - and if you use a stream, it must remain in existence for the lifetime of the image, which means that the original file is locked and cannot be opened again, either by your app or any other until the Image and it's underlying Stream has been Disposed.

This is generally a bad idea, and often leads to your app crashing because the file is in use (by your app) when the user tries to use it again ...

The solution is to create a copy of the file based image and use that:
Image result;
using (Image image = Image.FromFile(pathToImage))
    result = new Bitmap(image);
Don't forget to Dispose of the result Image when your app is finished with it! They use significant amounts of memory ...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900