newbie image question

J

jcreasy

So this is a really newbie question but I'll suck up my pride and
continue.

What is the best practice for including images, in this case .png's, in
a project. This is so I can do some fancy stuff with my GUI. I have a
bunch of .png's which I am setting as background images for controls. I
can add the image, set it to be an "embedded image" and use the
following line of code to set it as a background image.

<some control>.BackgroundImage = new Bitmap(typeof(myForm),
"imageName.png");

and that works just fine. The problem is I have a lot of images and a
lot of code files and it's just too dirty in my solution explorer. I
tried moving all of my images into a folder within the project, but the
only way I've been able to use my images is to have them copy to the
output folder and use the following line of code.

<some control>.BackgroundImage = new Bitmap("Images\\imageName.png");

The problem with this is the .exe has to be in the same folder as the
images folder. I prefer the embedded approach b/c all the user sees is
one .exe file.

So how do I use images in my project properly and keep it looking clean?
 
S

Stoitcho Goutsev \(100\)

jcreasy,

You can still move the images in a folder and set them as embedded resource.
Just when load the images add the name of the folder as part of the image
name. When C# VS IDE embeds the resources it first use the Default Name
Space from the projects settings and them all the folder names to the file
embedded as an resource. By default the Default Name Space is set to the
name space of the first created file by the wizard. Usually this is the
namespace of the main form and that's why code like
<some control>.BackgroundImage = new Bitmap(typeof(myForm),
"imageName.png"); works.

If you put the images in a folder called "Images" the code should look like

<some control>.BackgroundImage = new Bitmap(typeof(myForm),
"Images.imageName.png");

Keep in mind that this is how C# IDE embeds resources. VB.NET IDE is
different, which creates problems when using conversion utilites.
 
J

jcreasy

Huzzah, thank you very much Stoitcho. I was using a double slash
instead of the period

"Images\\imageName.png"

It's working great now. My OCD is very thankful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top