K
kurt sune
Anybody has a tip of where to find info/example how to retrieve an icon
from a .RESX-file?
/k
from a .RESX-file?
/k
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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 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
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.