Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello Everyone,

I have two problems with my Winforms application.

In my app im reading from a folder that contains 1000 photos.

1. Each time Im running my app, I programed it to read the photos stored in a specific folder and put them in a DB (Dictionary(string,image)), using:

C#
dI = new DirectoryInfo(@"C:\Users\Me\Desktop\Photos");
fileInfos = dI.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
foreach (FileInfo fi in fileInfos)
{
    DB.Add(Path.GetFileNameWithoutExtension(fi.FullName), Image.FromFile(fi.FullName));
}


but when im trying to place a photo in a pictureBox using:
pictureBox4.BackgroundImage =  DB["PhotoName"];


It shows nothing, blank.

that's a drawback, CAUSE,

2. I want to use this app in other computers using only the .exe file that stored in the ..\bin\debug\
without each time changing the path the photos are stored...

So, how do I create an .exe file for my app that already has all the photos? without worring about them? (problem no' 2)

And, how to make sure the photos are shown (problem no' 1)

Thank you all!
Posted
Updated 28-Mar-13 6:08am
v3
Comments
[no name] 28-Mar-13 12:14pm    
1. Probably because you do not have an image named "PhotoName" in the dictionary. And because it's a dictionary, you would want the value, I think.
2. You would either let the user choose a directory to show images from (preferred), or use a well known standard directory for the pictures that the user would have to use, or store images in a directory relative to your program.
RoyDoron 28-Mar-13 12:20pm    
1. I have all the photos. I checked many times. and tried other photos..
2. what is option no' 3 you said? "store images in a directory relative to your program" where exactly?
[no name] 28-Mar-13 12:40pm    
Your #1 has nothing to do with anything.
2. means exactly that. Store your images in a path that is relative to your program. Exactly where is up to you. I would have no idea how your program or your system or your users system is setup.

The chances are that the image is being shown - it certainly is here when I try your code.
But - are you sure you want it in the BackgroundImage? Because if you show a different image in the Image property, you probably won't see your background one...Have you tried setting teh Image property instead?


BTW: if you always want the same images, I would build them in as embedded resources to your EXE instead of loading them as separate files.
 
Share this answer
 
Comments
RoyDoron 28-Mar-13 13:16pm    
hey OriginalGriff,

No, there is no other photo in the background.
Thats the thing, the photos are changing so embedded is not the solution.
OriginalGriff 28-Mar-13 15:04pm    
If they are changing, then don't stick them in the bin directory - you may need admin permissions to change them. See here:
http://www.codeproject.com/Tips/370232/Where-should-I-store-my-data
Try using the Image property, or a panel and painting the image yourself (I would probably use that method myself, rather than a PictureBox as it uses fewer resources and can be quicker)
RoyDoron wrote:
how do I create an .exe file for my app that already has all the photos? without worring about them? (problem no' 2)


You can use resources. Adding and Editing resources[^] is very easy ;)

Embeding Image Resources in a project[^]
Code: Retrieving an Image that is an Embedded Resource (Visual C#)[^]
 
Share this answer
 
v2
Comments
RoyDoron 28-Mar-13 13:18pm    
Maciej Los,

what is the thing with this link.. how it helps me??
and the photos are changing so embedded is not the solution.
Maciej Los 28-Mar-13 13:23pm    
This is my answer for your question: how do I create an .exe file for my app that already has all the photos without worring about them? (problem no' 2)
If you want precise answer, ask precise question.
RoyDoron 28-Mar-13 13:33pm    
Is this the way to permanenty add the photo folder to my app?
There is nothing in my resource window..
how do I add the folder as a resource?
(note that in my project folder, under Resource there is the photo folder)
Maciej Los 28-Mar-13 15:33pm    
See my updated answer ;)
Sergey Alexandrovich Kryukov 1-Apr-13 10:47am    
Second one answered, a 5.
—SA

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