pitaya4
pitaya4

Reputation: 63

How to load background image from setting?

Before I get into the actual problem, I've done some research and nothing helps. Also, this isn't a duplicate of this question because s/he was using ComboBox, and this question is using an Open file dialog which does not help me.

Here is my problem. I am doing a Virtual System Overlay (some call it vb.net os) and for the "desktop" I need an background image loaded from an application setting. The problem is that I am getting this error:

An unhandled expection of type 'System.IO.FileNotFounfExpection' occurred in System.Drawing.dll

Additional information: My.Resources._2

And my code is:

Image.FromFile(My.Settings.bgdesk.ToString)

The setting looks like:

Name: bgdesk | Type: string | Scope: user | Value: My.Resources._2

Also, have a look at my resources:

Example of resources

How do I load the background image from the resource?

Upvotes: 2

Views: 2493

Answers (1)

Raffaello
Raffaello

Reputation: 112

If I properly understood your question, you want to dynamically load and save a background image setting, based on a user choice; the bitmaps to be used are already included in a resource file:

1) Create a Resource file named Resource1.resx
2) Insert bitmaps you want to use (e.g. named "_1", "_2"), in this file
3) In bgdesk, let the code save only the resource name (i.e. "_1", "_2")
4) Add a ComboBox cbImgList
5) Add a Button, which sets the new BackGround image when hit
6) Use the following code (adapt it to your needs)

Private Function GetBackGroundImgFromRes(ByRef sError As String) As Boolean

    Try

        Dim sImgName As String = My.Settings.bgdesk
        If sImgName.Trim <> "" Then
            Dim RM As Resources.ResourceManager = My.Resources.Resource1.ResourceManager
            Dim objBitmap As Object = RM.GetObject(sImgName)
            Me.BackgroundImage = DirectCast(objBitmap, Bitmap)
        End If

        Return True

    Catch ex As Exception

        sError = ex.ToString
        Return False

    End Try

End Function

Private Function SetBackGroundImg(ByRef sError As String) As Boolean

    Try

        Dim sImgName As String = ""
        Select Case cbImgList.SelectedIndex
            Case -1
                Return True
            Case 0
                sImgName = "_1"
            Case 1
                sImgName = "_2"
        End Select

        Dim RM As Resources.ResourceManager = My.Resources.Resource1.ResourceManager
        Dim objBitmap As Object = RM.GetObject(sImgName)
        Me.BackgroundImage = DirectCast(objBitmap, Bitmap)

        My.Settings.bgdesk = sImgName

        Return True

    Catch ex As Exception

        sError = ex.ToString
        Return False

    End Try

End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim sError As String = ""
    SetBackGroundImg(sError)

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    cbImgList.Items.Add("_1")
    cbImgList.Items.Add("_2")

    Dim sError As String = ""
    GetBackGroundImgFromRes(sError)

End Sub

Upvotes: 3

Related Questions