Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / Visual Basic
Article

Using embedded resources with .NET

Rate me:
Please Sign up or sign in to vote.
2.72/5 (22 votes)
30 Mar 2003 115.3K   597   27   9
Using embedded resources with .NET.

Sample Image - EmbeddedIconDemo.gif

Introduction

Embedded resource in a project is a new thing in .NET. This sample shows how to use it.

Using the code

The form has a button which changes the form icon.

VB
Dim oStream As System.IO.Stream
Dim oAssembly As System.Reflection.Assembly
Dim sIcon As String
Dim oBitmap As Bitmap

If vbON Then
    sIcon = "EmbeddedIconDemo.LIGHTON.ICO"
Else
    sIcon = "EmbeddedIconDemo.LIGHTOFF.ICO"
End If

'open the executing assembly
oAssembly = System.Reflection.Assembly.LoadFrom(Application.ExecutablePath)

'create stream for image (icon) in assembly
oStream = oAssembly.GetManifestResourceStream(sIcon)

'create new bitmap from stream
oBitmap = CType(Image.FromStream(oStream), Bitmap)

'create icon from bitmap
FetchIcon = Icon.FromHandle(oBitmap.GetHicon)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
I started with VB4 in 1996, after that VB5 and VB6. Always advanced database applications (Oracle, Access and SQL-Server). The last 5 years as a technical architect and project manager for development teams.

Special interest for user interaction.

Comments and Discussions

 
GeneralMy vote of 5 Pin
drummerboy051125-Mar-11 11:32
professionaldrummerboy051125-Mar-11 11:32 
Generalhello^^ Pin
Demantorr17-Jan-10 13:06
Demantorr17-Jan-10 13:06 
GeneralWrong title Pin
-R-y-a-n-28-Oct-07 1:25
-R-y-a-n-28-Oct-07 1:25 
GeneralAlways getting error when added new icon Pin
yj80018-Aug-07 22:39
yj80018-Aug-07 22:39 
Hi,

I have added another icon to your project and changed the Build Action of it to Emmbedded resource but I always get error on this line of code:

'create new bitmap from stream
oBitmap = CType(Image.FromStream(oStream), Bitmap)

saying Parameter is not valid.

I have run the other code suggested here to find out the exact case sensitive name of my resource that is EmbeddedIconDemo.BRANO.ico

I have even made the program to use it from the variable so there cant be any typo error but I am still getting this.

I have also tried creating a new project but exactly the same is happening even if I use your icons they dont work outside your project...

Any help appreciated Cool | :cool:


bpajer@gmail.com
GeneralThis worked for me :) Pin
3DWebGuy9-Jun-05 10:32
3DWebGuy9-Jun-05 10:32 
GeneralSame problem with cursors Pin
mfortin4-Sep-03 22:52
mfortin4-Sep-03 22:52 
Generalshowhelp embedded resource chm file Pin
mdg1710-Jul-03 8:08
mdg1710-Jul-03 8:08 
GeneralMinor Improvement Pin
Sander Schreurs31-Mar-03 4:55
Sander Schreurs31-Mar-03 4:55 
GeneralTell More Pin
Poolbeer30-Mar-03 19:43
Poolbeer30-Mar-03 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.