Load GIF/JPG File From Resource File

You can use resource file to save multiple Images files, Cursors, Sound files, and all other files in one single file.
This example will show you how to save and load BMP, Cur, Ico, GIF and JPG files from Resource Files.

Preparations

Add 1 Picture Box to your form.

Add the Resource Editor Add-in To your project: From VB Menu (VB 6.0) choose Add-Ins->Add-In Manager, then select the VB6 Resource Editor, mark the Loaded/Unloaded check Box, and press OK.

Now, to launch the Resource Editor, from the VB Menu choose Tools -> Resource Editor.

To add BMP, Ico or Cur files, simply select from the Resource Editor menu Add Cursor, Add Icon or Add Bitmap. Then select your BMP, Ico or Cur file, and press Open.
By default it will be saved under 101 ID. You can change the ID to other name, for example "Door-Image" Via the Properties option in the Resource Editor menu.

You can repeat the last operation to add more files to the resource file. Every file has its own ID, so you will be able to access it by its unique ID.

If you will add Bitmap, it will be saved in the resource file under the Bitmap "Folder" (It's not really folder, because it's one single file), If you will add Icon, it will be saved under Icon "Folder", and so on.

To add GIF or JPG file to the resource file, Choose in the Resource Editor Menu 'Add Custom Resource' And choose your GIF\JPG File. It will be saved under CUSTOM "Folder".

Now press 'Save' in the Resource Editor Menu, Enter your wanted resource file name and press Save.

As you can see, the resource file has been added to your project: From VB menu choose View->Project Explorer, and by default under the Related Documents folder, you will see the file Project1.RES (or if you saved the resource file in other name, yourResourceFileName.RES).

If you start a new project, and you want to add this specific resource file to your project, choose from VB Menu Project->Add new resource file, and select this file. Note that "Add new resource file" menu item will appear only if you add the Resource Editor Add-in (That I'd explained how to add it in the beginning of this article).

To load BMP file from the resource file to a picture box (named Picture1), simply type:
Picture1.Picture = LoadResPicture(101, vbResBitmap)

vbResBitmap - because it's Bitmap file.
101 - because it's the ID of this specific Bitmap. If the ID was "MyImage", you should have been use:
Picture1.Picture = LoadResPicture("MyImage", vbResBitmap)

To load Icon file (With the ID 101) from the resource file simply type:
Picture1.Picture = LoadResPicture(101, vbResIcon)

To load Cursor file from the resource file to your Form MouseIcon property:
Form1.MouseIcon = LoadResPicture(101, vbResCursor)

(don't forget to set the form MousePointer property to 99-Custom).

There is no Built-In option to load GIF and JPG files. There is no vbResGIF or vbResJPG. So to load these files you'll have to use the following code:

Form Code

Private Sub Form_Load()
'Replace '101' with your File ID.
'Replace 'c:\tmp\mytmp' with the name of the Temporary file you want to save
'the image to.
   
LoadDataIntoFile 101, "c:\tmp\mytmp"
    Picture1.Picture = LoadPicture("c:\tmp\mytmp")
End Sub

Public Sub LoadDataIntoFile(DataName As Integer, FileName As String)
    Dim myArray() As Byte
    Dim myFile As Long
    If Dir(FileName) = "" Then
        myArray = LoadResData(DataName, "CUSTOM")
        myFile = FreeFile
        Open FileName For Binary Access Write As #myFile
        Put #myFile, , myArray
        Close #myFile
    End If
End Sub

Go Back