PureBasic:ImagePlugin


PureBasic:Manuel

<< Précédent | Sommaire | Suivant >>


[modifier] General

PureBasic supporte différents formats d'images grâce à un système de plugins dynamiques. Seul l'encodeur ou le décodeur nécessaire sera intégré au fichier éxecutable, réduisant ainsi considérablement sa taille. Par exemple, si vous avez seulement besoin du format JPEG, seule la routine concernant ce format sera utilisée. Une autre caractéristique intéressante est l'auto-détection du format de fichier, si plusieurs décodeurs sont utilisés. Les commandes suivantes supportent les plugins d'images: LoadImage(), CatchImage(), SaveImage(), LoadSprite() CatchSprite(), et SaveSprite().

[modifier] Sommaire des commandes

Exemple

;
; ------------------------------------------------------------
;
;   PureBasic - ImagePlugin example file
;
;    (c) 2001 - Fantaisie Software
;
; ------------------------------------------------------------
;

; Enable all the decoders than PureBasic actually supports
;
UseJPEGImageDecoder()
UseTGAImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()

; Enable all the encoders than PureBasic actually supports
;
UseJPEGImageEncoder()
UsePNGImageEncoder()


If OpenWindow(0, 0, 0, 250, 130, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "PureBasic - Image Converter")

  CreateGadgetList(WindowID())
    ImageGadget(0, 0, 28, WindowWidth(), WindowHeight(), 0, #PB_Image_Border)
    
  CreateToolBar(0, WindowID())
    ToolBarStandardButton(0, #PB_ToolBarIcon_Open)
    ToolBarStandardButton(1, #PB_ToolBarIcon_Save)
    DisableToolBarButton(1, 1)    ; disable the save button
  
  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Menu  ; ToolBar are acting as menu
    
      Select EventMenuID()
      
        Case 0  ; Open
        
          Filename$ = OpenFileRequester("Choose a picture", "", "All Images Formats|*.bmp;*.jpg;*.png;*.tif;*.tga", 0)
          If Filename$
          
            If LoadImage(0, Filename$)
              SetGadgetState(0, ImageID())  ; change the picture in the gadget
              DisableToolBarButton(1, 0)    ; enable the save button
              ResizeWindow(ImageWidth()+4, ImageHeight()+34)
            EndIf
          
          EndIf
        
        Case 1  ; Save
          
          Filename$ = SaveFileRequester("Save a picture", Left(Filename$, Len(Filename$)-Len(GetExtensionPart(Filename$))-1), "BMP Format|*.bmp|JPEG Format|*.jpg|PNG Format|*.png", 0)
          If Filename$
          
            Select SelectedFilePattern()
            
              Case 0  ; BMP
                ImageFormat = #PB_ImagePlugin_BMP
                Extension$  = "bmp"

              Case 1  ; JPEG
                ImageFormat = #PB_ImagePlugin_JPEG
                Extension$  = "jpg"
                
              Case 2  ; PNG
                ImageFormat = #PB_ImagePlugin_PNG
                Extension$  = "png"

            EndSelect
            
            If LCase(GetExtensionPart(Filename$)) <> Extension$
              Filename$ + "." + Extension$
            EndIf
            
            If SaveImage(0, Filename$, ImageFormat)
              MessageRequester("Information", "Image saved successfully", 0)
            EndIf
          
          EndIf
      
      EndSelect
    
    EndIf
    
  Until EventID = #PB_EventCloseWindow  ; If the user has pressed on the close button
  
EndIf

End   ; All is automatically freed by PureBasic

[modifier] OS supportés

Windows, Linux, MacOS X