473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

adding an image resource

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
Nov 17 '05 #1
2 3440

"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

Nov 17 '05 #2

"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
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: D. Yates | last post by:
Hi, I am looking for an example of how to extract bitmap images from an embedded resource file (a file with *.res extension, which can be viewed inside of the ide and can hold bitmaps, icons,...
3
by: Bern | last post by:
This message is probably out of topic but I get no response from the less popular microsoft.public.dotnet.general newsgroup. Is there a resource manager of some sort in VS C# .NET which you can...
0
by: Josef Meile | last post by:
Hi, I'm working with .resx files for a multilanguage application and so far it has worked perfectly. However, now I want to add more attributes to the xml schema and read them at running time....
3
by: Shawn | last post by:
Hi. I have an asp:table, like this: <asp:Table runat="server" id="Table1"> <asp:TableRow Runat="server"> <asp:TableCell id="tc1" Runat="server">&nbsp;</asp:TableCell> <asp:TableCell id="tc2"...
8
by: Matt | last post by:
I've been searching around for a simple way to display a background image in my MDI form's client area. I want to be able to load the image from file on start up and have it resize as the MDI form...
3
by: UJ | last post by:
I've got an image I want to embed in a dll to use on a screen later. I've got it in a resource file, got it to compile in to the dll. The problem is getting it back out. It seems like my problem is...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
3
by: crazychrisy54 | last post by:
Hi there I just wondered if there is any way using GD to insert a clickable button or some clickable text into a image? It is possible to create images for buttons but what if you want a...
5
by: DBC User | last post by:
I have a situation, where I need to add 4 or 5 data files (they change every time I build) in my project during build time and somehow I need a way to access these files during runtime. So I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.