Using Embedded Resources - For Newbies

G

Guest

Thought the group might be interested in the following class(modified from an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a project and
embed it in the project by select "embedded Resource" from right clicking on
the icon1.ico in the solution Explorer listing. The following class can be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

'Resource Class
Imports System.Reflection
Public Class Resources
Shared v_AssemblyName As String
Shared initialize As Boolean
Public Sub New()
If Not initialize Then
Dim thisExe As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly()
v_AssemblyName = thisExe.GetName.Name & "."
initialize = True
End If
End Sub

Public Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & IconName & ".ico"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
Else
Return Nothing
End If
End Function

Public Function GetImage(ByVal ImageName As String) As Image
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & ImageName & ".bmp"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
Else
Return Nothing
End If
End Function

End Class
 
O

One Handed Man \( OHM - Terry Burns \)

Nice one, I'm going to use that in an application I'm writing.

Thanks for sharing it with the group :)


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
G

Guest

You're welcome. Hope we have gracefully agreed to disagree on the spelling
issue...we all have our own opinions.

One Handed Man ( OHM - Terry Burns ) said:
Nice one, I'm going to use that in an application I'm writing.

Thanks for sharing it with the group :)


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dennis said:
Thought the group might be interested in the following class(modified from
an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a project
and
embed it in the project by select "embedded Resource" from right clicking
on
the icon1.ico in the solution Explorer listing. The following class can
be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

'Resource Class
Imports System.Reflection
Public Class Resources
Shared v_AssemblyName As String
Shared initialize As Boolean
Public Sub New()
If Not initialize Then
Dim thisExe As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly()
v_AssemblyName = thisExe.GetName.Name & "."
initialize = True
End If
End Sub

Public Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & IconName & ".ico"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
Else
Return Nothing
End If
End Function

Public Function GetImage(ByVal ImageName As String) As Image
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & ImageName & ".bmp"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
Else
Return Nothing
End If
End Function

End Class
 
L

Larry Serflaten

Dennis said:
Thought the group might be interested in the following class(modified from an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a project and
embed it in the project by select "embedded Resource" from right clicking on
the icon1.ico in the solution Explorer listing. The following class can be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

That's a good idea. Might I suggest a little improvemant may make it more
versatile. As you show, you have to create the resource before you can use it,
but it may be useful to use it in this manner:


Private bmp As Bitmap = MyResource.GetBitmap("mountains")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.Icon = MyResource.GetIcon("Mountain.ico")
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
e.Graphics.DrawImageUnscaled(bmp, 0, 0)
End Sub

Note the bmp variable is declared at the module level, where the resource class
could not be created.... To cover that usage, you could make those functions
shared also. Here is another go at that class with a few additions:

HTH
LFS



Public Class MyResource

Public Shared Function AssemblyName() As String
Return System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
End Function

Public Shared Function GetIcon(ByVal Name As String) As System.Drawing.Icon
' *** NAME is CASE SENSITIVE ***
If Name.Length > 0 Then
Dim Filename As String
If InStr(LCase(Name), ".ico") > 0 Then
Filename = Name
Else
Filename = Name & ".ico"
End If
Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
AssemblyName() & "." & Filename)
If Not res Is Nothing Then
GetIcon = New Icon(res)
res.Close()
End If
End If
End Function


Public Shared Function GetBitmap(ByVal Name As String) As System.Drawing.Bitmap
' *** NAME is CASE SENSITIVE ***
If Name.Length > 0 Then
Dim Filename As String
If InStr(LCase(Name), ".bmp") > 0 Then
Filename = Name
Else
Filename = Name & ".bmp"
End If
Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
AssemblyName() & "." & Filename)

If Not res Is Nothing Then
GetBitmap = DirectCast(Bitmap.FromStream(res), Bitmap)
res.Close()
End If
End If
End Function


End Class
 
O

One Handed Man \( OHM - Terry Burns \)

I don't remember what spelling issues your talking about, unless your they
guy I had a big argument with about how important spelling was some two or
three months ago, in which case jus ignore me as I was simply being awkward
for the hell of it.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dennis said:
You're welcome. Hope we have gracefully agreed to disagree on the
spelling
issue...we all have our own opinions.

One Handed Man ( OHM - Terry Burns ) said:
Nice one, I'm going to use that in an application I'm writing.

Thanks for sharing it with the group :)


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dennis said:
Thought the group might be interested in the following class(modified
from
an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a
project
and
embed it in the project by select "embedded Resource" from right
clicking
on
the icon1.ico in the solution Explorer listing. The following class
can
be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

'Resource Class
Imports System.Reflection
Public Class Resources
Shared v_AssemblyName As String
Shared initialize As Boolean
Public Sub New()
If Not initialize Then
Dim thisExe As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly()
v_AssemblyName = thisExe.GetName.Name & "."
initialize = True
End If
End Sub

Public Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & IconName & ".ico"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
Else
Return Nothing
End If
End Function

Public Function GetImage(ByVal ImageName As String) As Image
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & ImageName & ".bmp"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
Else
Return Nothing
End If
End Function

End Class
 
G

Guest

I'm the one...cheers.

One Handed Man ( OHM - Terry Burns ) said:
I don't remember what spelling issues your talking about, unless your they
guy I had a big argument with about how important spelling was some two or
three months ago, in which case jus ignore me as I was simply being awkward
for the hell of it.

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Dennis said:
You're welcome. Hope we have gracefully agreed to disagree on the
spelling
issue...we all have our own opinions.

One Handed Man ( OHM - Terry Burns ) said:
Nice one, I'm going to use that in an application I'm writing.

Thanks for sharing it with the group :)


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Thought the group might be interested in the following class(modified
from
an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a
project
and
embed it in the project by select "embedded Resource" from right
clicking
on
the icon1.ico in the solution Explorer listing. The following class
can
be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

'Resource Class
Imports System.Reflection
Public Class Resources
Shared v_AssemblyName As String
Shared initialize As Boolean
Public Sub New()
If Not initialize Then
Dim thisExe As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly()
v_AssemblyName = thisExe.GetName.Name & "."
initialize = True
End If
End Sub

Public Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & IconName & ".ico"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
Else
Return Nothing
End If
End Function

Public Function GetImage(ByVal ImageName As String) As Image
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim Name As String = v_AssemblyName & ImageName & ".bmp"
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
Else
Return Nothing
End If
End Function

End Class
 
G

Guest

Thanks...that does make it easier to use. I'm just not a very experienced
programmer (just a hobbiest and not much time to devote to it).

Larry Serflaten said:
Dennis said:
Thought the group might be interested in the following class(modified from an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a project and
embed it in the project by select "embedded Resource" from right clicking on
the icon1.ico in the solution Explorer listing. The following class can be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

That's a good idea. Might I suggest a little improvemant may make it more
versatile. As you show, you have to create the resource before you can use it,
but it may be useful to use it in this manner:


Private bmp As Bitmap = MyResource.GetBitmap("mountains")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.Icon = MyResource.GetIcon("Mountain.ico")
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
e.Graphics.DrawImageUnscaled(bmp, 0, 0)
End Sub

Note the bmp variable is declared at the module level, where the resource class
could not be created.... To cover that usage, you could make those functions
shared also. Here is another go at that class with a few additions:

HTH
LFS



Public Class MyResource

Public Shared Function AssemblyName() As String
Return System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
End Function

Public Shared Function GetIcon(ByVal Name As String) As System.Drawing.Icon
' *** NAME is CASE SENSITIVE ***
If Name.Length > 0 Then
Dim Filename As String
If InStr(LCase(Name), ".ico") > 0 Then
Filename = Name
Else
Filename = Name & ".ico"
End If
Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
AssemblyName() & "." & Filename)
If Not res Is Nothing Then
GetIcon = New Icon(res)
res.Close()
End If
End If
End Function


Public Shared Function GetBitmap(ByVal Name As String) As System.Drawing.Bitmap
' *** NAME is CASE SENSITIVE ***
If Name.Length > 0 Then
Dim Filename As String
If InStr(LCase(Name), ".bmp") > 0 Then
Filename = Name
Else
Filename = Name & ".bmp"
End If
Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
AssemblyName() & "." & Filename)

If Not res Is Nothing Then
GetBitmap = DirectCast(Bitmap.FromStream(res), Bitmap)
res.Close()
End If
End If
End Function


End Class
 
G

Guest

Larry, just one question. You noted that the name was case sensitive. Was
this only to determine if the name had the .ico or .bmp extension or does
that mean that the whole resource name is Case Sensitive, i.e., will
"icon1.ico" get a resource that was loaded named "ICON1.ICO"? Thanks again
for the tips.

Larry Serflaten said:
Dennis said:
Thought the group might be interested in the following class(modified from an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc. Suppose you add an icon named "Icon1" to a project and
embed it in the project by select "embedded Resource" from right clicking on
the icon1.ico in the solution Explorer listing. The following class can be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

That's a good idea. Might I suggest a little improvemant may make it more
versatile. As you show, you have to create the resource before you can use it,
but it may be useful to use it in this manner:


Private bmp As Bitmap = MyResource.GetBitmap("mountains")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.Icon = MyResource.GetIcon("Mountain.ico")
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
e.Graphics.DrawImageUnscaled(bmp, 0, 0)
End Sub

Note the bmp variable is declared at the module level, where the resource class
could not be created.... To cover that usage, you could make those functions
shared also. Here is another go at that class with a few additions:

HTH
LFS



Public Class MyResource

Public Shared Function AssemblyName() As String
Return System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
End Function

Public Shared Function GetIcon(ByVal Name As String) As System.Drawing.Icon
' *** NAME is CASE SENSITIVE ***
If Name.Length > 0 Then
Dim Filename As String
If InStr(LCase(Name), ".ico") > 0 Then
Filename = Name
Else
Filename = Name & ".ico"
End If
Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
AssemblyName() & "." & Filename)
If Not res Is Nothing Then
GetIcon = New Icon(res)
res.Close()
End If
End If
End Function


Public Shared Function GetBitmap(ByVal Name As String) As System.Drawing.Bitmap
' *** NAME is CASE SENSITIVE ***
If Name.Length > 0 Then
Dim Filename As String
If InStr(LCase(Name), ".bmp") > 0 Then
Filename = Name
Else
Filename = Name & ".bmp"
End If
Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
AssemblyName() & "." & Filename)

If Not res Is Nothing Then
GetBitmap = DirectCast(Bitmap.FromStream(res), Bitmap)
res.Close()
End If
End If
End Function


End Class
 
L

Larry Serflaten

Dennis said:
Larry, just one question. You noted that the name was case sensitive. Was
this only to determine if the name had the .ico or .bmp extension or does
that mean that the whole resource name is Case Sensitive, i.e., will
"icon1.ico" get a resource that was loaded named "ICON1.ICO"? Thanks again
for the tips.


The name has to exactly match the name of the resource. In this case
'icon.ico' and 'ICON.ico' will not match and the method will fail. That's
why I thought it important to mention it right there in the methods....

LFS
 
G

Guest

Yeah, I found that out by trial and error. However, the case of the
extension it seems needs to be in lower case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top