Results 1 to 7 of 7

Thread: Can a picturebox....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Can a picturebox....

    I have a large number of images that I need in my VB program but they slow the application right down

    So instead of placing then on the form what I was wondering was if I placed all the required images in a file then at runtime somehow say along the lines of:

    If CheckBoxApple = true then
    Picture1.Picture = 'Apple.jpeg'
    end if

    Is something like this even possible to do?

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Can a picturebox....

    Put them in a resource file and load them as needed.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Can a picturebox....

    By 'resource file' do you mean a normal file placed in the same place as the application

    I.e. In a folder called 'My Program' I have my program that I have written then in the same folder I have a sub folder called 'Images'

    Sorry I sound thick

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Can a picturebox....

    Resource file is placed in your EXE. Try loading it as follows:
    Code:
    Picture1.Picture = LoadPicture (App.Path & "\Apple.jpeg")
    ... where 'Apple.jpeg' is a file in EXE's path.

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Can a picturebox....

    Take a look at the "Resource File Tutorial" in my signature for the basics. If you're storing jpegs, you need to add them as "Custom" resources. Unfortunately, you can't use "LoadResPicture" with jpegs, so they have to be extracted to (temporary) files (with LoadResData), then loaded into the picturebox (See gavio's code above). Once loaded, the (temporary) file can be deleted.

    It's annoying, but MSDN had an article, with code, that showed exactly how to do this, but I can't find it now . Looks like it'll have to be done the hard way - not that it's actually hard.
    Quote Originally Posted by bubblegum_girl
    Sorry I sound thick
    It's all relative. I'd no doubt sound thick to the Prime Minister, but he'd be a numpty at driving a bus.

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Can a picturebox....

    Quote Originally Posted by schoolbusdriver
    <snip>It's annoying, but MSDN had an article, with code, that showed exactly how to do this, but I can't find it now .
    I too had a look recently to no avail but I still have a copy of the code...
    Code:
    ' --------------------------------------------------------------------------
    '               Copyright (C) 1998 Microsoft Corporation                   '
    ' --------------------------------------------------------------------------
    ' You have a royalty-free right to use, modify, reproduce and distribute   '
    ' the Sample Application Files (and/or any modified version) in any way    '
    ' you find useful, provided that you agree that Microsoft has no warranty, '
    ' obligations or liability for any Sample Application Files.               '
    ' --------------------------------------------------------------------------
    ' Written by Mike Dixon ([email protected])                            '
    ' --------------------------------------------------------------------------
    Option Explicit
    
    Public Declare Function GetTempFilename Lib "kernel32" _
        Alias "GetTempFileNameA" ( _
        ByVal lpszPath As String, _
        ByVal lpPrefixString As String, _
        ByVal wUnique As Long, _
        ByVal lpTempFilename As String _
        ) As Long
    
    Public Declare Function GetTempPath Lib "kernel32" _
        Alias "GetTempPathA" ( _
        ByVal nBufferLength As Long, _
        ByVal lpBuffer As String _
        ) As Long
    
    
    Public Function LoadPictureResource( _
        ByVal ResourceID As Long, _
        ByVal sResourceType As String, _
        Optional TempFile _
        ) As Picture
        '=====================================================
        'Returns a picture object from a resource file.
        'Used for loading images other than ICO and BMP into a
        'Picture property. (Such as GIF and JPG images)
        '=====================================================
    
        'EXAMPLE CALL:
        'Set Picture1.Picture = LoadPictureResource(101, "Custom", "C:\temp\temp.tmp")
        Dim sFileName As String
        
        'Check if the TempFile Name has been specified
        If IsMissing(TempFile) Then
            'Create a temp file name such as "~res1234.tmp"
            GetTempFile "", "~rs", 0, sFileName
        Else
            'Use the specified temp file name
            sFileName = TempFile
        End If
        
        'Save the resource item to disk
        If SaveResItemToDisk(ResourceID, sResourceType, sFileName) = 0 Then
        
            'Return the picture
            Set LoadPictureResource = LoadPicture(sFileName)
            
            'Delete the temp file
            Kill sFileName
        End If
        
    End Function
    
    Public Function SaveResItemToDisk( _
                ByVal iResourceNum As Integer, _
                ByVal sResourceType As String, _
                ByVal sDestFileName As String _
                ) As Long
        '=============================================
        'Saves a resource item to disk
        'Returns 0 on success, error number on failure
        '=============================================
        
        'Example Call:
        ' iRetVal = SaveResItemToDisk(101, "CUSTOM", "C:\myImage.gif")
        
        Dim bytResourceData()   As Byte
        Dim iFileNumOut         As Integer
        
        On Error GoTo SaveResItemToDisk_err
        
        'Retrieve the resource contents (data) into a byte array
        bytResourceData = LoadResData(iResourceNum, sResourceType)
        
        'Get Free File Handle
        iFileNumOut = FreeFile
        
        'Open the output file
        Open sDestFileName For Binary Access Write As #iFileNumOut
            
            'Write the resource to the file
            Put #iFileNumOut, , bytResourceData
        
        'Close the file
        Close #iFileNumOut
        
        'Return 0 for success
        SaveResItemToDisk = 0
        
        Exit Function
    SaveResItemToDisk_err:
        'Return error number
        SaveResItemToDisk = Err.Number
    End Function
    
    Public Function GetTempFile( _
        ByVal strDestPath As String, _
        ByVal lpPrefixString As String, _
        ByVal wUnique As Integer, _
        lpTempFilename As String _
        ) As Boolean
        '==========================================================================
        ' Get a temporary filename for a specified drive and filename prefix
        ' PARAMETERS:
        '   strDestPath - Location where temporary file will be created.  If this
        '                 is an empty string, then the location specified by the
        '                 tmp or temp environment variable is used.
        '   lpPrefixString - First three characters of this string will be part of
        '                    temporary file name returned.
        '   wUnique - Set to 0 to create unique filename.  Can also set to integer,
        '             in which case temp file name is returned with that integer
        '             as part of the name.
        '   lpTempFilename - Temporary file name is returned as this variable.
        ' RETURN:
        '   True if function succeeds; false otherwise
        '==========================================================================
        
        If strDestPath = "" Then
            ' No destination was specified, use the temp directory.
            strDestPath = String(255, vbNullChar)
            If GetTempPath(255, strDestPath) = 0 Then
                GetTempFile = False
                Exit Function
            End If
        End If
        lpTempFilename = String(255, vbNullChar)
        GetTempFile = GetTempFilename(strDestPath, lpPrefixString, wUnique, lpTempFilename) > 0
        lpTempFilename = StripTerminator(lpTempFilename)
    End Function
    
    
    Public Function StripTerminator(ByVal strString As String) As String
        '==========================================================
        ' Returns a string without any zero terminator.  Typically,
        ' this was a string returned by a Windows API call.
        '
        ' IN: [strString] - String to remove terminator from
        '
        ' Returns: The value of the string passed in minus any
        '          terminating zero.
        '==========================================================
        
        Dim intZeroPos As Integer
    
        intZeroPos = InStr(strString, Chr$(0))
        If intZeroPos > 0 Then
            StripTerminator = Left$(strString, intZeroPos - 1)
        Else
            StripTerminator = strString
        End If
    End Function

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Can a picturebox....

    you could also store them in a imagelist.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width