473,372 Members | 868 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,372 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 3436

"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.