2

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?

Community
  • 1
  • 1
pitaya4
  • 63
  • 17
  • Try using `My.Resources` after saving whatever image you want on your resources folder – TGamer May 07 '17 at 10:37
  • @TGamer What do you mean? To load image directly image from code like `Me.BackgroundImage = My.Resources._2` ? Nope, I want the user to be able to choose one from a list with buttons, which will save the choice and load it next time. I have 9 images, not 1. – pitaya4 May 07 '17 at 10:42
  • 1
    I mean what you said just now. Use a `Select Case` to decide which one. – TGamer May 07 '17 at 11:25

1 Answers1

3

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
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Raffaello
  • 112
  • 4
  • Nope, you didn't understand my question. I DON'T want a combo box. I want like 9 buttons next to each other with sets new value in the setting. For example: Button 1 will set the setting's value to My.Resources._2 and buton 2 to My.Resources._3 .. I can do that but I can't ACTUALLY show the image as an background image. – pitaya4 May 07 '17 at 11:37
  • @DimitarTheN00b Look more closely at the `SetBackGroundImg` method - it contains what you need, Raffaello was kind enough to provide enough code and instructions for *a* complete working answer. – Andrew Morton May 07 '17 at 11:53
  • Ok I will try it now again. – pitaya4 May 07 '17 at 12:02
  • @AndrewMorton Haha it works now.. Sorry for being mean.. Just one more question: I am using different forms for my desktop and settings, how can I load the background to be in Form1.vb but not in Settings.vb ? Thanks. – pitaya4 May 07 '17 at 12:06
  • NVM figured it out. Marked as best answer. – pitaya4 May 07 '17 at 12:21