Get a file in resource folder based on a value of a variable RRS feed

  • Question

  • Hi

    I have a program in vb that has multiple buttons, and when one of them is pressed they change a variable's value (i ll call it i here)

    eg. button 1 is pressed, i = 1, button 2 is pressed, i = 2.

    Then there is a picturebox that get pictures from a local folder

    eg. button 1 is pressed, picturebox get ("location\on\my\computer\pic" & i & ".png"), so pic1.png is in the picturebox

    I realised that the program will not work if i publish it and put it on another computer because the files will not be there. So i imported the pictures in the resources folder but i dont think it works with my.resources, so is there a solution to make it work like the original code?

    Friday, May 10, 2019 8:10 AM

All replies

  • Hello,

    You could add the images to the project. Let's say you create a folder "\images"
    In the file from Solution Explorer, you can select the image, go to properties, and select "Copy Always"
    It means that when you publish your code, it will publish the images as well.

    And you can always load this image like ("images\pic" & i & ".png").

    Friday, May 10, 2019 8:38 AM
  • If you added the picture files to resources called “pic1”, “pic2”, etc., then check this example:


       Dim image = CType(My.Resources.ResourceManager.GetObject("pic" & i), Image)

       pictureBox.Image = image


    Friday, May 10, 2019 9:24 AM
  • Hello,

    Option 1

    Consider placing the images in a folder below the application folder e.g. Images. Then to get the path use this pattern

    Imports System.IO
    Public Class Form1
        Private ImageIndex As Integer = 0
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ImageIndex = 1
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ImageIndex = 2
        End Sub
        Private Sub readImageButton_Click(sender As Object, e As EventArgs) Handles readImageButton.Click
            Dim imageFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                             "Images", $"pic{ImageIndex}.png")
            If File.Exists(imageFileName) Then
                PictureBox1.Image = Image.FromFile(imageFileName)
            End If
        End Sub
    End Class

    Option 2 

    Place images in project resources as per below. In this case this is a working example where I picked images at random so they don't end with numbers but will work just the same with numbers as you create the image base name then get it. In this case I use a ComboBox where My.Images.BitmapNames is a List(Of String)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.DataSource = My.Images.BitmapNames
    End Sub

    To set an initial image in this case we find the image in My.Images.BitmapNames via lambda in form load

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PictureBox1.Image = My.Images.BitmapFromResource(ComboBox1.Text)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DataSource = My.Images.BitmapNames
            PictureBox1.Image = My.Images.BitmapFromResource(
                My.Images.BitmapNames.FirstOrDefault(Function(imgName) imgName = "BlackMiata"))
        End Sub
    End Class

    Add the following to your project as a class.

    Namespace My
        Partial Friend Class _MyImages
            Private MyProperties As Reflection.PropertyInfo()
            ''' <summary> 
            ''' Gets a image from resource or if not found a blank image 
            ''' </summary> 
            ''' <param name="sender">Name of an existing item in My.Resources</param> 
            ''' <returns></returns> 
            ''' <remarks> 
            ''' For this demo I return a transparent bitmap, feel free to change. 
            ''' </remarks> 
            Public Function BitmapFromResource(sender As String) As Bitmap
                Dim propertyInfo = (From T In MyProperties
                                    Where T.PropertyType Is GetType(Bitmap) AndAlso T.Name = sender).
                If propertyInfo IsNot Nothing Then
                    Return CType(Resources.ResourceManager.GetObject(propertyInfo.Name), Bitmap)
                    Dim bm As New Bitmap(256, 256)
                    Dim gr As Graphics = Graphics.FromImage(bm)
                    Return bm
                End If
            End Function
            ''' <summary> 
            ''' Returns a list of bitmap names in this project resources 
            ''' </summary> 
            ''' <value></value> 
            ''' <returns></returns> 
            ''' <remarks></remarks> 
            Public ReadOnly Property BitmapNames As List(Of String)
            Public Sub New()
                MyProperties = GetType(Resources.Resources).
                    GetProperties(Reflection.BindingFlags.NonPublic Or
                    Reflection.BindingFlags.Instance Or
                BitmapNames = (
                    From T In MyProperties Where T.PropertyType Is GetType(Bitmap) Select T.Name).ToList
            End Sub
        End Class
        Friend Module KP_Images
            Private instance As New ThreadSafeObjectProvider(Of _MyImages)
            ReadOnly Property Images() As _MyImages
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace

    Screenshot of above work.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, May 10, 2019 9:49 AM