gdi transparent question

Post Reply
User avatar
Silvio.Falconi
Posts: 7348
Joined: Thu Oct 18, 2012 7:17 pm

gdi transparent question

Post by Silvio.Falconi »

I load a png image with

oGraphics := Graphics():New( hDC )

oImage1:= GDIBmp():new(,::aNameImages[1] )
....
oGraphics:DrawImage( ::aImages , 1, aPos )

but gdiBmp then is not transparent

is there a command I not Know ?

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Antonio Linares
Site Admin
Posts: 44515
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: gdi transparent question

Post by Antonio Linares »

PNG images loaded with GDIBmp should display transparently by default when using Graphics:DrawImage(). Based on the FiveWin codebase, here's what you need to check:

Correct Usage Pattern (from samples/graphics/gdiplus3.prg:58-69):

Code: Select all | Expand

local hdc := oWnd:getdc()
local oGraphics := Graphics():New( oWnd:hDC ) 
local oImage := GDIBmp():new("C:\fwh\bitmaps\pngs\image2.png" )

oGraphics:DrawImage( oImage, 60, 60 )

oGraphics:destroy()
oWnd:releasedc()

Common Issues:

  1. Check your DrawImage call - Your code shows oGraphics:DrawImage( ::aImages, 1, aPos ) which looks incorrect. DrawImage expects:

    • First parameter: a GDIBmp object (not an array)

    • Second parameter: nTop (numeric)

    • Third parameter: nLeft (numeric)

    • Optional: nWidth, nHeight

      Should be: oGraphics:DrawImage( ::aImages[1], nTop, nLeft )

  2. Verify PNG has alpha channel - After loading, check:

    Code: Select all | Expand

    oImage1 := GDIBmp():new(, ::aNameImages[1] )
    IF !oImage1:IsAlphaChannel()
       // PNG doesn't have alpha, or loaded incorrectly
       oImage1:Set32Bits(.T.)
       oImage1:Conver24to32Alpha(.T.)  // Convert using top-left pixel as transparent color
    ENDIF
  3. Resource vs File loading - If loading from resources (second parameter), ensure PNG is embedded as RCDATA (type 10), not BITMAP (type 2). For file loading, use first parameter:

    Code: Select all | Expand

    oImage1 := GDIBmp():new("c:\path\image.png")  // File
    // OR
    oImage1 := GDIBmp():new(, "MYPNG")  // Resource (must be RCDATA type 10)
  4. Ensure high quality rendering:

    Code: Select all | Expand

    oGraphics := Graphics():New( hDC )
    oGraphics:SetHighQuality()  // Ensures proper alpha blending

The transparency should work automatically with GDI+ if the PNG has an alpha channel and you're using the correct syntax. Check your DrawImage parameters first.

regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply