VB6Parse / Library / Resources / loadrespicture

VB6 Library Reference

LoadResPicture Function

Returns a picture object (StdPicture) containing an image from a resource (.res) file.

Syntax

LoadResPicture(index, format)

Parameters

Return Value

Returns a StdPicture object: - Picture object containing the loaded image from resources - Object can be assigned to Picture properties of controls - Object implements IPicture interface - Returns Nothing if resource not found (some VB versions) - May raise error 32813 if resource not found

Remarks

The LoadResPicture function loads images from embedded resources:

Typical Uses

  1. Load Bitmap to PictureBox
Picture1.Picture = LoadResPicture(101, vbResBitmap)
  1. Load Icon to Image Control
Image1.Picture = LoadResPicture(102, vbResIcon)
  1. Load Form Background
Me.Picture = LoadResPicture("BACKGROUND", vbResBitmap)
  1. Load Cursor
Me.MousePointer = vbCustom
Me.MouseIcon = LoadResPicture(103, vbResCursor)
  1. Load Named Resource
imgLogo.Picture = LoadResPicture("LOGO", vbResBitmap)
  1. Conditional Image Loading
If mode = "dark" Then
Picture1.Picture = LoadResPicture(201, vbResBitmap)
Else
Picture1.Picture = LoadResPicture(101, vbResBitmap)
End If
  1. Button Icons
cmdSave.Picture = LoadResPicture(104, vbResIcon)
  1. Multiple Images in Loop
For i = 1 To 5
imgArray(i).Picture = LoadResPicture(100 + i, vbResBitmap)
Next i

Basic Examples

Example 1: Basic Picture Loading

' Load bitmap from resources
Picture1.Picture = LoadResPicture(101, vbResBitmap)

' Load icon
Image1.Picture = LoadResPicture(102, vbResIcon)

' Load using string name
Picture2.Picture = LoadResPicture("SPLASH", vbResBitmap)

Example 2: Form Initialization

Private Sub Form_Load()
' Load form background
Me.Picture = LoadResPicture(101, vbResBitmap)

' Load toolbar icons
cmdNew.Picture = LoadResPicture(201, vbResIcon)
cmdOpen.Picture = LoadResPicture(202, vbResIcon)
cmdSave.Picture = LoadResPicture(203, vbResIcon)
End Sub

Example 3: Error Handling

On Error Resume Next
Picture1.Picture = LoadResPicture(999, vbResBitmap)
If Err.Number = 32813 Then
MsgBox "Resource not found!", vbCritical
Err.Clear
ElseIf Err.Number <> 0 Then
MsgBox "Error loading resource: " & Err.Description, vbCritical
Err.Clear
End If

Example 4: Dynamic Loading

Dim imageID As Integer
imageID = 101 + selectedIndex
Picture1.Picture = LoadResPicture(imageID, vbResBitmap)

Common Patterns

Pattern 1: SafeLoadResPicture

Function SafeLoadResPicture(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByVal ctrl As Object) As Boolean
On Error Resume Next
Set ctrl.Picture = LoadResPicture(resID, resFormat)
SafeLoadResPicture = (Err.Number = 0)
Err.Clear
End Function

Pattern 2: PreloadResourcePictures

Dim preloadedPics() As StdPicture

Sub PreloadResourcePictures()
Dim i As Long
ReDim preloadedPics(1 To 5)

For i = 1 To 5
Set preloadedPics(i) = LoadResPicture(100 + i, vbResBitmap)
Next i
End Sub

Sub ShowPreloadedImage(ByVal index As Long)
If index >= 1 And index <= UBound(preloadedPics) Then
Set Picture1.Picture = preloadedPics(index)
End If
End Sub

Pattern 3: LoadResPictureWithDefault

Function LoadResPictureWithDefault(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByVal defaultID As Variant) As StdPicture
On Error Resume Next

Set LoadResPictureWithDefault = LoadResPicture(resID, resFormat)
If Err.Number <> 0 Then
Err.Clear
Set LoadResPictureWithDefault = LoadResPicture(defaultID, resFormat)
End If
End Function

Pattern 4: LoadResByName

Function LoadResByName(ByVal resName As String, _
ByVal resFormat As Integer) As StdPicture
On Error Resume Next
Set LoadResByName = LoadResPicture(resName, resFormat)

If Err.Number <> 0 Then
Debug.Print "Failed to load resource: " & resName
Set LoadResByName = Nothing
Err.Clear
End If
End Function

Pattern 5: ToggleResPicture

Dim currentState As Boolean

Sub ToggleResPicture()
If currentState Then
Picture1.Picture = LoadResPicture(101, vbResBitmap)
Else
Picture1.Picture = LoadResPicture(102, vbResBitmap)
End If
currentState = Not currentState
End Sub

Pattern 6: LoadThemeResources

Enum ThemeType
tmLight = 0
tmDark = 1
End Enum

Sub LoadThemeResources(theme As ThemeType)
Dim baseID As Integer
baseID = IIf(theme = tmDark, 200, 100)

Me.Picture = LoadResPicture(baseID + 1, vbResBitmap)
Picture1.Picture = LoadResPicture(baseID + 2, vbResBitmap)
Picture2.Picture = LoadResPicture(baseID + 3, vbResBitmap)
End Sub

Pattern 7: ResExists

Function ResExists(ByVal resID As Variant, _
ByVal resFormat As Integer) As Boolean
On Error Resume Next
Dim pic As StdPicture
Set pic = LoadResPicture(resID, resFormat)
ResExists = (Err.Number = 0)
Set pic = Nothing
Err.Clear
End Function

Pattern 8: LoadAllResourceIcons

Function LoadAllResourceIcons(startID As Integer, _
endID As Integer) As Collection
Dim col As New Collection
Dim i As Integer
Dim pic As StdPicture

On Error Resume Next
For i = startID To endID
Set pic = LoadResPicture(i, vbResIcon)
If Err.Number = 0 Then
col.Add pic
End If
Err.Clear
Next i

Set LoadAllResourceIcons = col
End Function

Pattern 9: SetButtonIcon

Sub SetButtonIcon(btn As CommandButton, _
ByVal iconID As Integer, _
ByVal enabled As Boolean)
On Error Resume Next

If enabled Then
Set btn.Picture = LoadResPicture(iconID, vbResIcon)
Else
Set btn.Picture = LoadResPicture(iconID + 100, vbResIcon)
End If

btn.enabled = enabled
End Sub

Pattern 10: LoadResourceArray

Sub LoadResourceArray(ByRef picArray() As StdPicture, _
ByVal startID As Integer, _
ByVal count As Integer, _
ByVal resFormat As Integer)
Dim i As Integer
ReDim picArray(1 To count)

On Error Resume Next
For i = 1 To count
Set picArray(i) = LoadResPicture(startID + i - 1, resFormat)
If Err.Number <> 0 Then
Debug.Print "Failed to load resource: " & (startID + i - 1)
Err.Clear
End If
Next i
End Sub

Advanced Examples

Example 1: Resource Picture Manager

' Class: ResPictureManager
Private m_cache As Collection

Private Sub Class_Initialize()
Set m_cache = New Collection
End Sub

Public Function LoadPicture(ByVal resID As Variant, _
ByVal resFormat As Integer) As StdPicture
Dim key As String
On Error Resume Next

key = CStr(resID) & "_" & CStr(resFormat)
Set LoadPicture = m_cache(key)

If Err.Number <> 0 Then
Err.Clear
Set LoadPicture = LoadResPicture(resID, resFormat)
If Err.Number = 0 Then
m_cache.Add LoadPicture, key
Else
Err.Raise vbObjectError + 1000, "ResPictureManager", _
"Failed to load resource"
End If
End If
End Function

Public Sub AssignToControl(ByVal ctrl As Object, _
ByVal resID As Variant, _
ByVal resFormat As Integer)
Set ctrl.Picture = LoadPicture(resID, resFormat)
End Sub

Public Sub ClearCache()
Dim i As Long
For i = m_cache.Count To 1 Step -1
m_cache.Remove i
Next i
End Sub

Public Property Get CacheSize() As Long
CacheSize = m_cache.Count
End Property

Private Sub Class_Terminate()
ClearCache
Set m_cache = Nothing
End Sub
' Form with Picture1, Timer1, cmdNext, cmdPrev, lblInfo
Private Const BASE_IMAGE_ID = 1001
Private Const IMAGE_COUNT = 10
Private currentIndex As Long

Private Sub Form_Load()
currentIndex = 0
ShowCurrentImage
Timer1.Interval = 5000 ' 5 seconds
Timer1.Enabled = True
End Sub

Private Sub ShowCurrentImage()
Dim imageID As Integer
On Error Resume Next

imageID = BASE_IMAGE_ID + currentIndex
Set Picture1.Picture = LoadResPicture(imageID, vbResBitmap)

If Err.Number <> 0 Then
Picture1.Cls
Picture1.Print "Image not found"
Else
lblInfo.Caption = "Image " & (currentIndex + 1) & " of " & IMAGE_COUNT
End If
Err.Clear
End Sub

Private Sub Timer1_Timer()
NextImage
End Sub

Private Sub cmdNext_Click()
NextImage
End Sub

Private Sub cmdPrev_Click()
PrevImage
End Sub

Private Sub NextImage()
currentIndex = (currentIndex + 1) Mod IMAGE_COUNT
ShowCurrentImage
End Sub

Private Sub PrevImage()
currentIndex = (currentIndex - 1 + IMAGE_COUNT) Mod IMAGE_COUNT
ShowCurrentImage
End Sub

Example 3: Toolbar with Resource Icons

' Form with toolbar buttons array: cmdTool(0 to 9)
Private Type ToolButton
caption As String
iconID As Integer
enabled As Boolean
tooltip As String
End Type

Private toolConfig() As ToolButton

Private Sub Form_Load()
InitializeToolbar
ApplyToolbarConfig
End Sub

Private Sub InitializeToolbar()
ReDim toolConfig(0 To 9)

With toolConfig(0)
.caption = "New"
.iconID = 201
.enabled = True
.tooltip = "Create new document"
End With

With toolConfig(1)
.caption = "Open"
.iconID = 202
.enabled = True
.tooltip = "Open existing document"
End With

With toolConfig(2)
.caption = "Save"
.iconID = 203
.enabled = False
.tooltip = "Save current document"
End With

' ... more buttons
End Sub

Private Sub ApplyToolbarConfig()
Dim i As Long
On Error Resume Next

For i = 0 To UBound(toolConfig)
With cmdTool(i)
.caption = toolConfig(i).caption
.enabled = toolConfig(i).enabled
.ToolTipText = toolConfig(i).tooltip

Set .Picture = LoadResPicture(toolConfig(i).iconID, vbResIcon)
If Err.Number <> 0 Then
Debug.Print "Failed to load icon: " & toolConfig(i).iconID
Err.Clear
End If
End With
Next i
End Sub

Public Sub EnableTool(ByVal index As Long)
If index >= 0 And index <= UBound(toolConfig) Then
toolConfig(index).enabled = True
cmdTool(index).enabled = True
End If
End Sub

Public Sub DisableTool(ByVal index As Long)
If index >= 0 And index <= UBound(toolConfig) Then
toolConfig(index).enabled = False
cmdTool(index).enabled = False
End If
End Sub

Example 4: Multi-State Indicator

' Form with imgStatus (Image control)
Public Enum StatusState
stIdle = 0
stProcessing = 1
stSuccess = 2
stWarning = 3
stError = 4
End Enum

Private Const RES_STATUS_IDLE = 301
Private Const RES_STATUS_PROCESSING = 302
Private Const RES_STATUS_SUCCESS = 303
Private Const RES_STATUS_WARNING = 304
Private Const RES_STATUS_ERROR = 305

Private statusIcons() As StdPicture
Private currentStatus As StatusState

Private Sub Form_Load()
LoadStatusIcons
SetStatus stIdle
End Sub

Private Sub LoadStatusIcons()
ReDim statusIcons(0 To 4)

On Error Resume Next
Set statusIcons(stIdle) = LoadResPicture(RES_STATUS_IDLE, vbResIcon)
Set statusIcons(stProcessing) = LoadResPicture(RES_STATUS_PROCESSING, vbResIcon)
Set statusIcons(stSuccess) = LoadResPicture(RES_STATUS_SUCCESS, vbResIcon)
Set statusIcons(stWarning) = LoadResPicture(RES_STATUS_WARNING, vbResIcon)
Set statusIcons(stError) = LoadResPicture(RES_STATUS_ERROR, vbResIcon)

If Err.Number <> 0 Then
MsgBox "Warning: Some status icons could not be loaded", vbExclamation
Err.Clear
End If
End Sub

Public Sub SetStatus(ByVal newStatus As StatusState)
currentStatus = newStatus

If newStatus >= 0 And newStatus <= UBound(statusIcons) Then
If Not statusIcons(newStatus) Is Nothing Then
Set imgStatus.Picture = statusIcons(newStatus)
End If
End If
End Sub

Public Function GetStatus() As StatusState
GetStatus = currentStatus
End Function

Private Sub Form_Unload(Cancel As Integer)
Dim i As Long
For i = 0 To UBound(statusIcons)
Set statusIcons(i) = Nothing
Next i
End Sub

Error Handling

' Error 32813: Resource not found
On Error Resume Next
Set pic = LoadResPicture(999, vbResBitmap)
If Err.Number = 32813 Then
MsgBox "Resource not found!"
End If

' Error 48: Error loading from file
Set pic = LoadResPicture(101, vbResBitmap)
If Err.Number = 48 Then
MsgBox "Resource file is corrupt or missing!"
End If

' Safe loading pattern
Function TryLoadResPicture(ByVal resID As Variant, _
ByVal resFormat As Integer, _
ByRef pic As StdPicture) As Boolean
On Error Resume Next
Set pic = LoadResPicture(resID, resFormat)
TryLoadResPicture = (Err.Number = 0)
Err.Clear
End Function

Performance Considerations

Best Practices

  1. Always handle errors - resource might not exist
  2. Use constants for resource IDs for maintainability
  3. Preload frequently used images for better performance
  4. Release memory by setting picture objects to Nothing when done
  5. Use meaningful names for string-based resource IDs
  6. Test all resources during development
  7. Document resource IDs in code or separate file
  8. Use Resource Editor to manage resources efficiently
  9. Consider image size - large bitmaps increase EXE size
  10. Cache in Collection for images used multiple times
Function Purpose Source External Files
LoadResPicture Load from resources Embedded .res No
LoadPicture Load from file External file Yes
LoadResData Load binary data Embedded .res No
LoadResString Load string Embedded .res No

LoadResPicture vs LoadPicture

' LoadResPicture - from embedded resources
Picture1.Picture = LoadResPicture(101, vbResBitmap)

' LoadPicture - from external file
Picture1.Picture = LoadPicture("C:\Images\logo.bmp")

When to use each: - LoadResPicture: Distribution (no external files), static images, faster loading - LoadPicture: Dynamic images, user-selected files, easier updates

Resource Format Constants

' Format constants
Const vbResBitmap = 0  ' Bitmap (.bmp)
Const vbResIcon = 1    ' Icon (.ico)
Const vbResCursor = 2  ' Cursor (.cur)

' Usage
Picture1.Picture = LoadResPicture(101, vbResBitmap)
Image1.Picture = LoadResPicture(102, vbResIcon)
Me.MouseIcon = LoadResPicture(103, vbResCursor)

Platform Notes

Limitations

← Back to Resources | View all functions