"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Don Tucker" <Do*******@discussions.microsoft.com> wrote in message
news:18**********************************@microsof t.com... Hello, I have a Windows Application that needs to load multiple image
files
in response to users clicking buttons. Currently, I just have these
images
as .jpg files on disk, but I don't want to have to migrate the files
everywhere that the executable is installed. Is there a way to add them
to a
Visual Studio solution/project so that they are embedded in the
application
somehow? If so, how do I access the images when needed?
Thanks,
Don
Add Existing Item (and add the images wherever you want them in your
project, a folder perhaps).
Open the property sheet for each item and change the Build Action property
value to "Embedded Resource".
When you build the project, it will compile the images into the solution.
You will have to get the full resource name of the image(s). You can do
this using the following code and hard-code the names, or store them
elsewhere for use in your prod app:
Assembly asm = Assembly.GetAssembly(this);
string[] names = asm.GetManifestResourceNames();
foreach (string name in names) {
Console.WriteLine(name);
}
The above was done in a Console app, btw. The full path should be
FULL NAMESPACE.ProjectFolderPaths.FileName.Extension
Then you can access those resources with the following code:
public Image GetImageResource(string ResourceName)
{
Assembly asm = Assembly.GetAssembly(this);
Stream stream = asm.GetManifestResourceStream(ResourceName);
return Image.FromStream(stream);
}
and call it with:
Image img = GetImageResource("mynamespace.images.myimage.bmp") ;
HTH :)
Mythran
You can also do it without reflection by having an abstract form with the
embedded images. This works well if your app has forms that are all
basically a template of some base form. The template or base form will have
form objects of all the images you might want to use. They'll be placed in
standard locations according to your app. When you are ready to have a form
use this standard, just copy all of the form objects from the base and paste
them onto your next form. Delete those objects you do not need for this
particular form.
The base form is an abstract:
public abstract class BaseForm : System.Windows.Forms.Forms
Then override all of the other forms' OnLoad methods. Call base.Onload():
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(BaseImageF orm);
this.SomeImageName.Image =
((System.Drawing.Image)(resources.GetObject("SomeI mageName.Image")));
}
In the designer, I have a picture box on the above form named SomeImageName.
I set the Image property to none since that is assigned at runtime.
Using the above technique will greatly decrease the file size of each form
and therefore your EXE. It also implements a standard for each form. You
can make it more precise by creating a method that assigns sizes and
locations to all of your form objects.
Brett