How to Load GIF/JPG file from Resource file
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 'File/Folder Handling - How to Load GIF/JPG file from Resource file Option Explicit '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. 'Add a Picture Box to your form. 'Add the Resource Editor Add-in to your project. To do that, GoTo 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 "Main-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. 'As you can see, the resource file has been added to your project 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. 'To load BMP file from the resource file to a picture box, 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 "YourImage", you 'should have been use: Picture1.Picture = LoadResPicture("YourImage", 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) 'You need to set the Form MousePointer property to 99-Custom to see the changes. '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: 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(ByVal DataName As Integer, ByVal sFileName As String) Dim myArray() As Byte Dim myFile As Long If Dir(sFileName) = "" Then myArray = LoadResData(DataName, "CUSTOM") myFile = FreeFile Open sFileName For Binary Access Write As #myFile Put #myFile, , myArray Close #myFile End If End Sub |