Search code examples
.netvb.netreferencesettingsembedded-resource

How to reference resources to save in settings - Visual Basic 2008 .net


I'm trying to save a setting that keeps what background image I'm currently using.

If it's an external file, there is no problem, i can just get the .location and set it in a String setting to store it.

But, if I want to store an image resource as a setting, how do i do it?

I know i can store rawdata, like bitmap information, but then i can't set that type of data to the my.background.

I don't know if the question should be: "how do i reference resources", but the my.resources.resource.methodsList doesnt contain anything that seems to be of use.


Solution

  • You can use My.Resources.ResourceManager.GetObject("resource name") to read a resource by string. Here's some example code that shows you how to get a list of all the embedded images from the resources which includes both their string name and the image object itself:

    Public Class EmbeddedImage
        Public Sub New(ByVal name As String, ByVal image As Image)
            _name = name
            _image = image
        End Sub
    
        Public ReadOnly Property Name() As String
            Get
                Return _name
            End Get
        End Property
        Private _name As String
    
        Public ReadOnly Property Image() As Image
            Get
                Return _image
            End Get
        End Property
        Private _image As Image
    End Class
    
    Private Function getEmbeddedImages() As List(Of EmbeddedImage)
        Dim images As List(Of EmbeddedImage) = New List(Of EmbeddedImage)()
        Dim resources As ResourceSet = My.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
        For Each resource As DictionaryEntry In resources
            If TypeOf resource.Value Is Image Then
                images.Add(New EmbeddedImage(resource.Key.ToString(), CType(resource.Value, Image)))
            End If
        Next
        Return images
    End Function