Try this class to retrieve embedded resources. Note that the names of the
files are CASE sensitive including the .ico or .bmp endings.
Imports System
Imports System.Reflection
Imports System.Drawing
Imports System.Windows.Forms
Public Class Resources
'WARNING: Icon and Image Names are Case Sensistive
Private Shared Function AssemblyName() As String
Static v_AssemblyName As String
If v_AssemblyName Is Nothing Then v_AssemblyName =
System.Reflection.Assembly.GetExecutingAssembly().GetName.Name & "."
Return v_AssemblyName
End Function
Public Shared Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = IconName
If InStr(v_name.ToLower, ".ico") <= 0 Then v_name = v_name & ".ico"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
End If
End Function
Public Shared Function GetImage(ByVal BitMapName As String) As Bitmap
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = BitMapName
If InStr(v_name.ToLower, ".bmp") <= 0 Then v_name = v_name & ".bmp"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
End If
End Function
Public Shared Function GetCursor(ByVal CursorName As String) As Cursor
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = CursorName
If InStr(v_name.ToLower, ".cur") <= 0 Then v_name = v_name & ".cur"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Cursor(s)
s.Close()
End If
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
--
Dennis in Houston
Lou said:
This method does not seem to work or I can't get it to work?
I added an icon file to my project
I set it to "embedded resource" and added the code below in the proper
format but
it returns null.
There doesn't seem to be anything else to look for?
There must be something missing?
-Lou
if I add a new item (Solution Items) to my project and its an
icon(.ico),
how can I reference that file when I am coding
The easiest thing to do is as follows.
1. Add the .ico file(s) to your project.
2. For each icon file, set the build action to "embedded resource".
This
will include the .ico file in your .exe file (my direct experience is
only
with windows form .exe files, but I expect this will work with other
app
types).
3. In your program, you can get an icon object via the code fragment
shown
below. If the name of the icon file xxx.ico, and the name of your app
is
yyy, then the name of the icon resource in your .exe file is
"yyy.xxx.ico".
dim s as string = "yyy.xxx.ico" ' your names replace yyy and xxx
dim MyIcon = New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(s))
Now you can set a form's icon like: someform.Icon=MyIcon.