DrawImg

Syntax

DrawImg(Img, x.d, y.d, Width.d=#PG_Ignore, Height.d=#PG_Ignore, Opacity.f=1, Frame.i=#Null)

Description

Draws an image onto the current drawing target at the specified position, optionally scaling it to the given dimensions and applying opacity.

Parameters

Img
The handle of the image object (created with CreateImg() or LoadImg()) to draw.

x.d
The horizontal coordinate of the image's top-left corner in device-independent pixels (DIPs).

y.d
The vertical coordinate of the image's top-left corner in DIPs.

Width.d (optional)
The desired width to draw the image in DIPs. If #PG_Ignore or negative, the image's natural width (considering DPI) is used. Default is #PG_Ignore.

Height.d (optional)
The desired height to draw the image in DIPs. If #PG_Ignore or negative, the image's natural height (considering DPI) is used. Default is #PG_Ignore.

Opacity.f (optional)
The opacity to apply when drawing the image, ranging from 0.0 (fully transparent) to 1.0 (fully opaque). Default is 1.0.

Frame.i (optional)
For multi-frame images (like animated GIFs, although support might vary), the zero-based index of the frame to draw. Default is #Null (draws the first frame or the only frame).

Return Value

This command does not return a value. It returns internally if the Img handle is invalid or Opacity is zero or less.

Remarks

Coordinates and dimensions are in DIPs and are scaled according to the current drawing context's DPI. The function automatically selects the best matching alternate image size (if available via ImgAddSize()) based on the target drawing dimensions and DPI, improving quality when scaling. The interpolation mode used for scaling is determined by the image's properties (see ImgSetInterpolationMode()).

Example

IncludeFile "ProGUI_PB.pbi"

StartProGUI()

; Load an image
Global MyImage = LoadImg("icons/boingball.png") ; Make sure this image exists

If MyImage = 0
  MessageRequester("Error", "Could not load icons/boingball.png")
  End
EndIf

Procedure DrawHandler(Window, EventType, *EventData.PG_EventDraw, *UserData)
  
    DrawClear(RGB(220, 220, 220), 1) ; Light grey background

    ; Draw image at original size
    DrawImg(MyImage, 10, 10)

    ; Draw image scaled down
    DrawImg(MyImage, 150, 10, 50, 50)

    ; Draw image scaled up with 50% opacity
    DrawImg(MyImage, 10, 120, 200, 200, 0.5)

EndProcedure

MyWindow = CreateWindow(0, 0, 350, 350, "DrawImg Example")

If MyWindow
  AddEventHandler(MyWindow, #PG_Event_Draw, @DrawHandler())
  WindowShow(MyWindow, #True, #PG_Window_ScreenCentered)

  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf

FreeImg(MyImage)
StopProGUI()

See Also

LoadImg, CreateImg, FreeImg, DrawImgEx, ImgAddSize, ImgSetInterpolationMode, BeginDraw, EndDraw

Supported OS

Windows, Linux