DrawImgShadow

Syntax

DrawImgShadow(Img, x.d, y.d, Width.d=#PG_Ignore, Height.d=#PG_Ignore, BlurAmount.f=10, Spread.f=0, ShadowColor.l=0, ShadowOpacity.f=1, xOffset.d=0, yOffset.d=0, isHollow=#False)

Description

Draws a drop shadow for an image based on its alpha channel. The shadow is generated using the opaque and semi-transparent areas of the source image to create an accurate silhouette, which is then blurred and composited onto the current drawing target.

Parameters

Img
The handle of the image object (created with CreateImg() or LoadImg()) to generate a shadow from.

x.d, y.d
The horizontal and vertical coordinates of the top-left corner where the shadow calculation originates, in device-independent pixels (DIPs).

Width.d, Height.d (optional)
The target width and height to scale the image to before calculating the shadow. Uses the image’s original dimensions if #PG_Ignore is passed. Default is #PG_Ignore.

BlurAmount.f (optional)
The amount of Gaussian blur to apply to the shadow. Higher values create a softer, more dispersed shadow. Default is 10.

Spread.f (optional)
The expansion (positive value) or contraction (negative value) amount of the shadow’s footprint before blurring. Default is 0.

ShadowColor.l (optional)
The color of the shadow in RGB format (e.g., using RGB(r, g, b)). Default is 0 (Black).

ShadowOpacity.f (optional)
The opacity of the shadow, ranging from 0.0 (fully transparent) to 1.0 (fully opaque). Default is 1.0.

xOffset.d, yOffset.d (optional)
The horizontal and vertical offsets of the shadow relative to the specified x and y coordinates. Default is 0.

isHollow (optional)
If #True, the shadow will only be drawn outside the opaque bounds of the original image, masking out the inner region. This is useful for creating outline or glow effects where the source image shouldn’t multiply with the shadow underneath it. Default is #False.

Return Value

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

Remarks

The shadow coordinates, dimensions, offsets, blur amount, and spread are specified in DIPs but are scaled internally according to the current drawing context’s DPI. Note that DrawImgShadow only draws the shadow; it does not draw the original image. You typically follow a call to DrawImgShadow with a call to DrawImg() at the same x and y coordinates to place the image over its shadow.

Example

IncludeFile "ProGUI_PB.pbi"

StartProGUI()

; Create an image with a transparent background and draw a red ellipse onto it
Global MyImage = CreateImg(100, 100, #PG_Img_Transparent)
If BeginDraw(OutputImg(MyImage))
  DrawEllipse(50, 50, 40, 30, RGB(255, 0, 0), 1.0)
  EndDraw()
EndIf

Procedure DrawHandler(Window, EventType, *EventData.PG_EventDraw, *UserData)
  
    DrawClear(RGB(240, 240, 240), 1.0) ; Light gray background

    ; 1. Draw a standard drop shadow (Black, 15px blur, offset by 10,10)
    DrawImgShadow(MyImage, 30, 50, #PG_Ignore, #PG_Ignore, 15, 0, RGB(0, 0, 0), 0.6, 10, 10)
    ; Draw the image on top of its shadow
    DrawImg(MyImage, 30, 50)

    ; 2. Draw a hollow shadow (Blue, 10px blur, 5px spread, no offset, isHollow=#True)
    DrawImgShadow(MyImage, 180, 50, #PG_Ignore, #PG_Ignore, 10, 5, RGB(0, 0, 255), 0.8, 0, 0, #True)
    ; (We don't draw the image here to clearly show the hollow shadow effect acting as an outer glow)
    
EndProcedure

MyWindow = CreateWindow(0, 0, 350, 200, "DrawImgShadow Example")

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

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

FreeImg(MyImage)

StopProGUI()

See Also

DrawImgShadowEx, DrawImgShadowInset, DrawImg, BeginDraw, EndDraw

Supported OS

Windows, Linux