DrawImgEx

Syntax

DrawImgEx(Img, x.d, y.d, Width.d, Height.d, dx.d, dy.d, dWidth.d, dHeight.d, Opacity.f)

Description

Draws a specified portion of an image (source rectangle) onto a specified area of the current drawing target (destination rectangle), applying scaling and opacity. This allows for drawing sprites or sections of larger images.

Parameters

Img
The handle of the source image object.

x.d
The horizontal coordinate of the top-left corner of the source rectangle within the image, in device-independent pixels (DIPs).

y.d
The vertical coordinate of the top-left corner of the source rectangle within the image, in DIPs.

Width.d
The width of the source rectangle within the image, in DIPs. Use negative values for natural width.

Height.d
The height of the source rectangle within the image, in DIPs. Use negative values for natural height.

dx.d
The horizontal coordinate of the top-left corner of the destination rectangle on the drawing target, in DIPs.

dy.d
The vertical coordinate of the top-left corner of the destination rectangle on the drawing target, in DIPs.

dWidth.d
The desired width of the destination rectangle on the drawing target, in DIPs. Use negative values for natural width (scaled from source).

dHeight.d
The desired height of the destination rectangle on the drawing target, in DIPs. Use negative values for natural height (scaled from source).

Opacity.f
The opacity to apply when drawing, ranging from 0.0 (fully transparent) to 1.0 (fully opaque).

Return Value

This command does not return a value.

Remarks

All coordinates and dimensions are in DIPs and are scaled according to the current drawing context's DPI. The source rectangle (x, y, Width, Height) defines the portion of the `Img` to draw. The destination rectangle (dx, dy, dWidth, dHeight) defines where and how large that portion should appear on the target. If `dWidth` or `dHeight` differ from `Width` or `Height` respectively, the image portion will be scaled. The interpolation mode used for scaling is determined by the image's properties.

Example

IncludeFile "ProGUI_PB.pbi"

StartProGUI()

; Load an image (assume it's at least 100x100)
Global MyImage = LoadImg("icons/boingball.png") ; Make sure this image exists

If Not MyImage
  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 the top-left 50x50 DIPs portion of the image
    ; at position (10, 10) with its original size
    DrawImgEx(MyImage, 0, 0, 50, 50, 10, 10, 50, 50, 1.0)

    ; Draw the same 50x50 source portion, but scaled up to 100x100
    ; at position (100, 10) with 70% opacity
    DrawImgEx(MyImage, 0, 0, 50, 50, 100, 10, 100, 100, 0.7)

    ; Draw a different portion (bottom-right 50x50 if image is 100x100)
    ; scaled down to 30x30 at position (10, 120)
    SharedImageWidth = ImgGetWidth(MyImage)
    SharedImageHeight = ImgGetHeight(MyImage)
    DrawImgEx(MyImage, SharedImageWidth-50, SharedImageHeight-50, 50, 50, 10, 120, 30, 30, 1.0)

EndProcedure

MyWindow = CreateWindow(0, 0, 250, 200, "DrawImgEx 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

DrawImg, LoadImg, CreateImg, BeginDraw, EndDraw

Supported OS

Windows (Direct2D only)