LoadImage
LoadImage Filename$, ImageNumber, [Format =1]
 
Parameters:

    Filename$ = The Filename and path of the image you wish to load
    ImageNumber = The Image Number where you wish to store/reference this picture data
    [Format =1] = Format of image to create, Format defaults to Video (1), use 2 for FX and 8 for AFX
Returns: NONE
 

      LoadImage reads a picture file from disc into PlayBASICs image memory. You must provide this command with the full folder path and filename of the image you wish to load, as well as a free (not currently in use) image index.

      If you wish PlayBASIC to manage indexes for you, see LoadNewImage



Image Format Parameter:


      PlayBASIC has various types of images, each one is suited for different uses.

           1 = Video - Image is created in Video Memory
           2 = FX - System Memory Image
           8 = AFX - System Memory Image With Alpha Channel


      To learn more about images we highly recommend you read the CreateImage, Images & Sprites tutorials



FACTS:


      * LoadImage supports various popular image formats including BMP, TGA JPEG, PNG as well PlayBASIC very own Image Format called PBI

      * Images loaded with LoadImage are stored in your computers Video memory by default (Mode 1). Use the Format parameter to select the select that format. The only exception to this is when loading JPG images, which will always default to video.

      * Images stored Video Memory are most suitable for fast static (no rotation or alpha) drawing. Video Images are drawn using your computers Video Card (GPU). Since the image data is stored in graphics memory, they can not withstand breaks in service from your operating system, such as, when the user presses ALT-TAB, or when the OS decides to hibernate while your program is running. If a break of service occurs, you should reload/recreate these images.

      * LoadImage does not change PlayBASIC current graphics output. So if you want to redirect drawing operations onto a freshly loaded image, you'll have to do this manually with RenderToImage command.

      * LoadImage (and variants) can now load PBI / BMP / TGA + PNG's files directly from memory. To do so, the address of the data is given as a String in place of the filename with an & symbol prefix. eg LoadImage "&"+Str$(Ptr),Index - They can also be automatically loaded from the resource buffer. See #AddResource and Example #2 bellow




Mini Tutorial #1:


      This example loads an image from disc into image slot (index) #1 in memory, displays it to the screen at 100x, 100y then waits for the user to press a key before ending


(Sample ONLY)
  
; Load an image into image slot #1
  LoadImage "ThePictureYouWishToLoad.bmp",1
  
; Draw the Loaded image to the screen
  DrawImage 1,100,100,0
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  









Mini Tutorial #2: Load Image From Memory


      This example shows how we can use the LoadImage command to load from memory and not just disc. To demonstrate this, we first need to store an image in memory to load. The image load from memory routines expect a pointer to a the size of the image (stored as an integer) followed by the the image data. So an images that's 10000 bytes long, requires an integer header making it 10004 bytes in memory. In order to load it, we would give the loader the address of the size integer and as long as the image data is after in, it should load ok.

      Loading images from memory is normally used for data security purposes, which is where a programmer would store first pre-process their media files and store them in an encrypted/scrambled state. The program/game then loads the encrypted media files into memory, decrypts/descrambles the data in memory and then loads that media from memory directly. This saves ever having to store the decrypted media data in a temporary state on disc.

  
  
  // compute the location of a known image in
  // the help file folder
  Path$=ProgramDir$()+"Help\Commands/Media/"
  file$=path$+"ship.bmp"
  
  
  // Check if this file exists
  If FileExist(file$)
     
     // Get the size of this file in bytes
     Size=FileSize(File$)
     
     
     // Create a memory bank the size of the
     // file plus and extra 4 bytes for the length
     // header
     ThisBank= NewBank(Size + 4)
     
     // Get a pointer to the bank
     Ptr=GetBankPtr(ThisBank)
     
     // store the size of this image before the
     // image data
     PokeInt Ptr,Size
     
     fh=ReadNewFile(file$)
     If fh
        // Read the file into our memory bank
        ReadMemory fh,Ptr+4,Size
        CloseFile fh
        
        // Load IMage from Memory
        LoadImage "&"+Str$(ptr),1
        
        // Display the image
        DrawImage 1,100,100,false
        
        
     EndIf
     
  EndIf
  
  Sync
  WaitKey
  
  

 
Example Source: Download This Example
; Init a variable FileName$ to store the
; location and name of the image we wish to load
  FileName$="..\../Media/bubble_64x64.bmp"
  
; Load this file into IMage slot #1
  LoadImage Filename$,1
  
; draw this image to the screen
  DrawImage 1,350,200,false
  
; Display the screen and wait for a key press
  Sync
  WaitKey
  
  
  
  
 
Related Info: CreateFxImage | CreateFxImageEx | CreateImage | DeleteImage | DrawImage | GetFreeImage | GetImage | LoadFxImage | LoadNewImage | NewFxImage | NewImage | RenderToImage | SaveBitmap | SavePBI | Sprites :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com