Close AD
Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net) - Chintan Patel's Blog

Chintan Patel's Blog
Home | Archive | Links | Contact Sign In | Sign Up

About Author

Chintankumar Patel
09 Jun, 2008

Contact Me  

Working as a Technical Consultant for Conchango.

Having experience in to IT from 7+ years and working on Microsoft Technologies

Archive

2008 Oct   (3)
2008 Sep   (6)
2008 Aug   (1)
2008 Jul   (2)
2008 Jun   (7)

Recent Posts

What's New in the .NET Framework 2.0 ?
Comments : 0
Not Rated  
How to work with partitions in Windows Vista / XP when Disk Management doesn’t work
Comments : 0
Not Rated  
How to resize a partition in Windows Vista?
Comments : 0
Not Rated  
Top 10 tricks for handling null values in Microsoft Office Access
Comments : 0
Not Rated  
What is Vista's ReadyBoost and SuperFetch Technology
Comments : 0
Not Rated  
What is YouTube? - An introduction to the YouTube.com
Comments : 1
Not Rated  
SQL DATEDIFF Function - Applies to MS SQL Server and MS Office Access
Comments : 0
Not Rated  
Booting from USB Pen/Key/Flash Drive (Windows/Linux)
Comments : 1
Not Rated  
How to Convert FAT/FAT32 to NTFS file system
Comments : 0
Not Rated  
Resizing Images to match it's Scale in C# .Net
Comments : 5
Not Rated  

Categories

.Net Graphics   (2)
.Net Technology   (6)
ASP.Net   (1)
General   (1)
Microsoft Access   (1)
Microsoft Visual Studio   (1)
Microsoft Windows   (4)
SQL   (1)
USB   (1)
Windows Vista   (1)

Tags

.Net , .Net Framework 2.0 , .Net Graphics , Asp.Net , Boot , C# , Class Library , Coding Standards , Convert File System , Database , DATEDIFF , DATEDIFF Function , Disk Management , Embedded Resources , EnableEventValidation , EnableViewState , EncoderParameter , FAT32 to NTFS , Form Authentication , HDD , High Quality Thumbnail , Intellisense , Linux , Microsoft Access , Microsoft's SQL , Partition , ReadyBoost , ReSharper , Resize Image , Resize Partition , Security , SQLDataReader , SuperFetch , USB , Usb device not recognized , Vista , Visual Studio 2005 , What's New , Windows , Windows Application , Windows Errors , Windows Vista , Windows XP , YouTube

Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net)

Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net)

Wed, 11 Jun, 2008

In .Net when you don’t want to relay for a file on physical location, then it is very good option to take an advantage of Embedded resources.

Using embedded resources you can add any file type in the assembly/DLL/EXE when they get compiled. And whenever you want to use it, load it from the assembly, the files get stored in the metadata of Assembly.

Here is an example of how to use Embedded Resources.

1.
Open Microsoft Visual Studio and create new project for C# Windows Application, here I have created Windows Application named “EmbeddedTest”, even you can use “Class Library” 2.
To add a file in the project Right click on the project name in Solution Explorer, select “Add” >> “Existing Item…”

Uploaded Image

3.
Now we have added a file to the project, so it doesn’t mean that it will automatically embedded in the Assembly, for that we need to change the files “Build Action” property to “Embedded Resource”, and compiler will include this file in metatdata. Here I have added Image file(dog.jpg), you can add one or more any kind of files


Uploaded Image

4.
Now use the following code in Form1_Load method to list all the Embedded Resources available in the Assembly

private void Form1_Load(object sender, EventArgs e){Assembly a = Assembly.GetExecutingAssembly(); string[] resources = a.GetManifestResourceNames(); foreach (string resourceName in resources){listBox1.Items.Add(resourceName);}}


5.
The following code is used to get the resource from the Manifest of the Assembly when user clicks on the listBox1

private void listBox1_SelectedIndexChanged(object sender, EventArgs e){if (listBox1.SelectedIndex == -1)return; string selectedResourceName = listBox1.SelectedItem.ToString(); Assembly a = Assembly.GetExecutingAssembly(); Stream stream = a.GetManifestResourceStream(selectedResourceName); if (stream != null){pictureBox1.Image = null;try{Bitmap bmp = Bitmap.FromStream(stream) as Bitmap; pictureBox1.Image = bmp;}catch (Exception ex){MessageBox.Show("Selected item is not Image");} }}


In Step 5 the we first got the Stream by using GetManifestResourceStream() and then after we load it in to Bitmap object, if in between error occurs when the Resource is not type of Bitmap an Exception is thrown and error message is displayed.

Download Full Source Code

Download Project EmbeddedTest.zip (349.79 KB)

Download Project With Example of using Class Library (866.59 KB)



Uploaded Image

 


  

Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net)


Comments


Re : Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net)
Tue, 08 Jul, 2008 - 00:05:31 by kay maf
     i like this article, but can you do example of the same with Class library instead of window application. i embedded some images in class library that compiled to dll,the problem is i can't read the image file from the dll. it works with window application. i want to use the class library with embedded images with my window application. help pls
 
Re : Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net)
Tue, 08 Jul, 2008 - 21:49:32 by Chintankumar Patel
     Hiee... Kay

I have attached one more example with this post, This shows how to retrieve embeded images from the Class Library...

You may download "Example of using Class Library"
 
Re : Adding/Using Embedded Resources in .Net Windows Application/Class Library (C#/VB.Net/ASP.Net)
Fri, 12 Sep, 2008 - 08:47:35 by John Marshall
     I only know Visual Basic. can this be ported to VB?
 

Add Comment
 
 
 
 
 
 
 
 

Your Email address will not be displayed on the website, It is optional, if provided you will get an alert when some comments on this Post.
 
 
 
 
Remaining 5000
 

Powered by NineOn Inc.