Load icon from resx

  • Thread starter Thread starter kurt sune
  • Start date Start date
K

kurt sune

Anybody has a tip of where to find info/example how to retrieve an icon
from a .RESX-file?

/k
 
Dim p As System.Reflection.Assembly
p = System.Reflection.Assembly.GetExecutingAssembly()
Icons(0) = New System.Drawing.Icon(p.GetManifestResourceStream(Me.GetType(),
"GreenBulb.ico"))
Icons(1) = New System.Drawing.Icon(p.GetManifestResourceStream(Me.GetType(),
"YellowBulb.ico"))
Icons(2) = New System.Drawing.Icon(p.GetManifestResourceStream(Me.GetType(),
"RedBulb.ico"))

Hope it helps
Chris

PS. Make sure you make the icons as embedded.
 
Kurt,
In addition to the other comments.

Rather then storing the icon in a .RESX file, I normally just embed the icon
directly, as its easier to maintain.

Shawn's article shows how to embed the icon directly & retrieve it. Plus
explains why its easier to maintain.
http://www.windojitsu.com/blog/resxsucks.html

My Xml Resource Resolver was written with XML in mind, however it can easily
be used with other resources, such as icons.
http://msmvps.com/jayharlow/archive/2005/01/24/33766.aspx

To use my XmlResourceResolver "as is" with an icon, use something like:

Public Module TestModule
Dim name As String
Dim resolver As New XmlResourceResolver(GetType(TestModule))
Dim absoluteUri As Uri = resolver.ResolveUri(Nothing, name)
Dim input As Stream = DirectCast(resolver.GetEntity(absoluteUri,
Nothing, Nothing), Stream)

Dim theIcon As New Icon(input)

End Module

Alternatively you could just extract most of the GetEntity method into its
own class (which gives you a class similar to Shawn's). Something like:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Globalization
Imports System.Reflection

Public Class ResourceLoader

Private ReadOnly m_assembly As [Assembly]
Private ReadOnly m_type As Type

Public Sub New(ByVal type As Type)
m_assembly = type.Assembly
m_type = type
End Sub

Public ReadOnly Property Location() As String
Get
Return m_assembly.Location
End Get
End Property

Public Function GetStream(ByVal name As String) As Stream
Dim culture As CultureInfo = CultureInfo.CurrentUICulture
Dim stream As Stream

' Try the specific culture
stream = GetManifestResourceStream(culture, name)

' Try the neutral culture
If stream Is Nothing AndAlso Not culture.IsNeutralCulture Then
stream = GetManifestResourceStream(culture.Parent, name)
End If

' Try the default culture
If stream Is Nothing Then
stream = GetManifestResourceStream(name)
End If

If stream Is Nothing Then
Throw New FileNotFoundException(Nothing, name)
End If
Return stream

End Function

Private Function GetManifestResourceStream(ByVal culture As CultureInfo,
ByVal name As String) As Stream
Try
Dim satellite As [Assembly] =
m_assembly.GetSatelliteAssembly(culture)
Return satellite.GetManifestResourceStream(m_type, name)
Catch ex As FileNotFoundException
Return Nothing
End Try
End Function

Private Function GetManifestResourceStream(ByVal name As String) As
Stream
Return m_assembly.GetManifestResourceStream(m_type, name)
End Function

End Class



Hope this helps
Jay
 
I get error on Me.GetType:

"'Me' is valid only within an instance method."

It have a console application, thats why?


/k
 
Wow, quite an improvement versus the old LoadResIcon("iconname").

Thanks,
/k


Jay B. Harlow said:
Kurt,
In addition to the other comments.

Rather then storing the icon in a .RESX file, I normally just embed the icon
directly, as its easier to maintain.

Shawn's article shows how to embed the icon directly & retrieve it. Plus
explains why its easier to maintain.
http://www.windojitsu.com/blog/resxsucks.html

My Xml Resource Resolver was written with XML in mind, however it can easily
be used with other resources, such as icons.
http://msmvps.com/jayharlow/archive/2005/01/24/33766.aspx

To use my XmlResourceResolver "as is" with an icon, use something like:

Public Module TestModule
Dim name As String
Dim resolver As New XmlResourceResolver(GetType(TestModule))
Dim absoluteUri As Uri = resolver.ResolveUri(Nothing, name)
Dim input As Stream = DirectCast(resolver.GetEntity(absoluteUri,
Nothing, Nothing), Stream)

Dim theIcon As New Icon(input)

End Module

Alternatively you could just extract most of the GetEntity method into its
own class (which gives you a class similar to Shawn's). Something like:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Globalization
Imports System.Reflection

Public Class ResourceLoader

Private ReadOnly m_assembly As [Assembly]
Private ReadOnly m_type As Type

Public Sub New(ByVal type As Type)
m_assembly = type.Assembly
m_type = type
End Sub

Public ReadOnly Property Location() As String
Get
Return m_assembly.Location
End Get
End Property

Public Function GetStream(ByVal name As String) As Stream
Dim culture As CultureInfo = CultureInfo.CurrentUICulture
Dim stream As Stream

' Try the specific culture
stream = GetManifestResourceStream(culture, name)

' Try the neutral culture
If stream Is Nothing AndAlso Not culture.IsNeutralCulture Then
stream = GetManifestResourceStream(culture.Parent, name)
End If

' Try the default culture
If stream Is Nothing Then
stream = GetManifestResourceStream(name)
End If

If stream Is Nothing Then
Throw New FileNotFoundException(Nothing, name)
End If
Return stream

End Function

Private Function GetManifestResourceStream(ByVal culture As CultureInfo,
ByVal name As String) As Stream
Try
Dim satellite As [Assembly] =
m_assembly.GetSatelliteAssembly(culture)
Return satellite.GetManifestResourceStream(m_type, name)
Catch ex As FileNotFoundException
Return Nothing
End Try
End Function

Private Function GetManifestResourceStream(ByVal name As String) As
Stream
Return m_assembly.GetManifestResourceStream(m_type, name)
End Function

End Class



Hope this helps
Jay



kurt sune said:
Anybody has a tip of where to find info/example how to retrieve an icon
from a .RESX-file?

/k
 
In your code
"Return MyBase.ResolveUri(baseUri, relativeUri)"

I get errror:
"'MyBase' cannot be used with method 'Public Overridable MustOverride
Overloads Function ResolveUri(baseUri As System.Uri, relativeUri As String)
As System.Uri' because it is declared 'MustOverride'."

Ideas?

/k
 
Kurt,
BTW: If you want to keep the Icon in a .resx file, you can use a
System.Resources.ResourceManager to read it.

There are advantages to both.

Hope this helps
Jay

kurt sune said:
Wow, quite an improvement versus the old LoadResIcon("iconname").

Thanks,
/k


Jay B. Harlow said:
Kurt,
In addition to the other comments.

Rather then storing the icon in a .RESX file, I normally just embed the icon
directly, as its easier to maintain.

Shawn's article shows how to embed the icon directly & retrieve it. Plus
explains why its easier to maintain.
http://www.windojitsu.com/blog/resxsucks.html

My Xml Resource Resolver was written with XML in mind, however it can easily
be used with other resources, such as icons.
http://msmvps.com/jayharlow/archive/2005/01/24/33766.aspx

To use my XmlResourceResolver "as is" with an icon, use something like:

Public Module TestModule
Dim name As String
Dim resolver As New XmlResourceResolver(GetType(TestModule))
Dim absoluteUri As Uri = resolver.ResolveUri(Nothing, name)
Dim input As Stream = DirectCast(resolver.GetEntity(absoluteUri,
Nothing, Nothing), Stream)

Dim theIcon As New Icon(input)

End Module

Alternatively you could just extract most of the GetEntity method into
its
own class (which gives you a class similar to Shawn's). Something like:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Globalization
Imports System.Reflection

Public Class ResourceLoader

Private ReadOnly m_assembly As [Assembly]
Private ReadOnly m_type As Type

Public Sub New(ByVal type As Type)
m_assembly = type.Assembly
m_type = type
End Sub

Public ReadOnly Property Location() As String
Get
Return m_assembly.Location
End Get
End Property

Public Function GetStream(ByVal name As String) As Stream
Dim culture As CultureInfo = CultureInfo.CurrentUICulture
Dim stream As Stream

' Try the specific culture
stream = GetManifestResourceStream(culture, name)

' Try the neutral culture
If stream Is Nothing AndAlso Not culture.IsNeutralCulture Then
stream = GetManifestResourceStream(culture.Parent, name)
End If

' Try the default culture
If stream Is Nothing Then
stream = GetManifestResourceStream(name)
End If

If stream Is Nothing Then
Throw New FileNotFoundException(Nothing, name)
End If
Return stream

End Function

Private Function GetManifestResourceStream(ByVal culture As CultureInfo,
ByVal name As String) As Stream
Try
Dim satellite As [Assembly] =
m_assembly.GetSatelliteAssembly(culture)
Return satellite.GetManifestResourceStream(m_type, name)
Catch ex As FileNotFoundException
Return Nothing
End Try
End Function

Private Function GetManifestResourceStream(ByVal name As String) As
Stream
Return m_assembly.GetManifestResourceStream(m_type, name)
End Function

End Class



Hope this helps
Jay



kurt sune said:
Anybody has a tip of where to find info/example how to retrieve an
icon
from a .RESX-file?

/k
 
kurt,
Which version of .NET?

The code posted & on my blog works in VS.NET 2003 (.NET 1.1).

I noticed my blog page, has "cut & paste" issues, are you certain you got
the code to look like my blog page?

Hope this helps
Jay
 

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

Back
Top