Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added a new folder (named Images) to my project, it contains some JPG files. Now I need to browse each image in that folder and load each one into an Image object before passing this to a PictureBox to show. Could you please provide me with some sample code doing that? I have seen some code using System.Reflection.Assembly class and Bitmap.FromStream method to get the image. But I think I didn't know all significant notes about this work and couldn't have gotten anything except null value streams.
Thank you so much!
Posted
Updated 13-May-11 3:35am
v2

Look here[^], a nice CodeProject member has written an article on it.
 
Share this answer
 
Comments
Rick Shaub 13-May-11 10:04am    
That article is a little dated and doesn't really show how to utilize the functionality of PictureBox objects.
Richard MacCutchan 13-May-11 11:03am    
Yes, I noticed it was not very recent but thought it may have offered a starting point.
Sergey Alexandrovich Kryukov 13-May-11 15:03pm    
It will do the job of course (my 5) but the other problem was project settings (to copy or embed).

Please see my answer and compare with OP's own answer.
--SA
Even easier:

C#
string myImagePath = @"images\whatever.jpg";
pictureBox1.Image = Image.FromFile(myImagePath);


Important: You have to make sure the build settings for your image files are set to:

Build: Content
Copy to Output Directory: Copy if Newer

Note: This is not the default setting.

If you don't have the file set to be copied to your output directory, you probably won't have acces to them at runtime.
 
Share this answer
 
v5
Comments
[no name] 13-May-11 10:22am    
Oh no! Please look at "added to the project" in the title of the question. First, I right clicked on my project in the Solution Windows, a menu popped up, I selected "Add new folder", then renamed the folder to "Images", then I right clicked on this folder, selected "Add existing items", a file openning window appeared and I selected all JPG files I wanted.
And I think we may have more than 1 way to load these image files, but one of them is using System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(...) ... Please complete it!
Thank you so much!
[no name] 13-May-11 10:26am    
Plus, that way will load the images using Bitmap.FromStream not Bitmap.FromFile or Image.FromFile!
Thank you!
Rick Shaub 13-May-11 10:31am    
Please read my answer carefully. It will work. You need to set the build options for each file to the correct settings. If you want to use reflection, I suggest you do the research yourself because it's less simple than this way.
[no name] 13-May-11 11:05am    
I have tried yours and FileNotFoundException was thrown! Your way may be fine if the folder "Images" lies in the root folder of the application (at compile time, it is the "bin\Debug" folder). Even with that way, the application has always to bring with it a folder of images to run properly! But by including that folder into the project, and selecting appropriate settings the application will run well alone! (all images have been embedded to the exe file when compiling! I have found the solution! Please see my answer below!
Rick Shaub 13-May-11 11:20am    
You have to do some work yourself. Obviously you have to have a legitimate file path. I'm sure you could handle figuring that part out.
You can do a different thing:


  1. Don't add image file to you project manually, just store it somewhere outside solution, to start from.
    Create .resx file.
  2. In .resx file designer Resource, use "Add existing file".
  3. Add all you image file. They will be automatically 1) physically copied to the same directory, 2) added to the project, 3) referenced in the .resx file by its relative file names accordingly. The image files won't get "Copy" build action in the project, so the build won't copy them in the output directory; the files will be embedded as resources.
  4. Look at the auto-generated C# file under the .resx file node. Locate generated static class and several static properties; the names of these properties will be similar to the file names of original image files.
  5. Use these properties!
  6. PROFIT!
  7. —SA
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-May-11 3:57am    
I agree this is the cleanest and simplest method +5.
Sergey Alexandrovich Kryukov 14-May-11 13:46pm    
Thank you, Richard.
--SA
[no name] 15-May-11 0:14am    
Thank you! Is there a sample demo app in this forums? Could you tell me why it is better to maintain the app with your solution than mine?
Thank you so much!
Sergey Alexandrovich Kryukov 15-May-11 0:30am    
I really prefer my solution. Demo does not help much, because by the text of resulting artifacts you don't see what steps to make, must of the information is about what steps to do with IDE. Just create some demo project and try to follow all those steps; it's easier to do steps than explain them.
--SA
Sergey Alexandrovich Kryukov 15-May-11 2:02am    
I explained it in detail in my comment to your solution.
--SA
My solution! I have discovered that I didn't change the value of "Build action" (one of properties of each images added to the project) to "Embedded Resource".
That's for when designing! The code is the same that I mentioned! We will use System.Reflection.Assembly class.
Step1: Load images into System.IO.Stream objects:
Suppose one of the images is "1.jpg", I have: System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication1.Images.1.jpg");
Step2:
if(stream == null) pictureBox1.Image = null;
else
pictureBox1.Image = Bitmap.FromStream(stream, true);

Note: "WindowsFormsApplication1" is the namespace encasing all classes in my project!
That's all! Not too difficult! One way is a reference, the other way is an embedding!
Thank you!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-May-11 15:01pm    
Hey! Look at my answer. In my way, you won't need to get resource stream by name (which is not very maintainable) -- auto-generated C# file will do it all for you.

I'm sure you'll like this way more.
--SA
Sergey Alexandrovich Kryukov 15-May-11 2:02am    
I can try to explain what's the problem with method and why it's better to use the way I described. This is maintainability. Look at the code -- there are so called immediate string constants (hard-coded), like "WindowsFormsApplication1.Images.1.jpg". All immediate constants should be be used (except very basic cases like 0, 1, null; even "" should not be used (must be string.Empty). If file name is changed or this string is misspelled -- compiler will not detect the problem. Who can guarantee it works? Just testing? Do be serious.

Using auto-generated *.cs resource file and class, we rely on automatic generation of the resource retrieval code, which is of no use per se, we need just resources. This file is automatically refreshed during design time as soon as we change anything in resource. At the same time, we cannot misspell the property -- compiler will show the bug immediately.

--SA
[no name] 15-May-11 4:57am    
Thank you! Nice to try it! It is worthy to write an article about it!

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