Jump to content

Working with transparent GIFs?


JFee
 Share

Recommended Posts

Basically I am looking to create a GIF file that is transparent with some text on it. I am trying to use GDIPlus functions to accomplish this, but I am almost at a loss of where to start.

First off, is this possible? Essentially I just need to be able to have the BITMAP handle have alpha channels right? Any suggestions are appreciated.

TIA

Regards,Josh

Link to comment
Share on other sites

Global Const $GUI_Size = 100
Global $GUI_X = @DesktopWidth / 2, $GUI_Y = 100


$hGUI = GUICreate("Bouncing Ball", $GUI_Size, $GUI_Size, $GUI_X, $GUI_Y, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetState(@SW_SHOW)

$Ball = GUICtrlCreatePic("ball.gif", 0, 0, $GUI_Size, $GUI_Size, -1, $GUI_WS_EX_PARENTDRAG)

will display a transparent .gif (with invisible areas)

taken from gravity.au3

tolle indicium

Link to comment
Share on other sites

Hmm... let me clarify. I am trying to create a transparent GIF file. I don't care whether or not it is visible, but I am trying to save a file that has alpha channel information in it. For instance when you open Photoshop, create an image with a transparent background and save it as a GIF, it creates exactly what I want to be able to do (only my program will be far simpler than photoshop is ;) ).

Regards,Josh

Link to comment
Share on other sites

Hmmm... yet the end result I want is so simple. I am literally taking a string from a database and placing it in the image to be saved. That saved image is then being used as a source for a video switcher (stats for TV to go ontop of animations).

Since all of the files will be 720x480 anyway, could I create a blank file in photoshop, save it (the transparent background) then load that with GDIPlus and manipulate that?

Regards,Josh

Link to comment
Share on other sites

I feel like this should do what I want it to, but the save is failing...

_GDIPlus_Startup ()
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\template.gif")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hBrush = _GDIPlus_BrushCreateSolid (0xff000000)
$hFormat = _GDIPlus_StringFormatCreate ()
$hFamily = _GDIPlus_FontFamilyCreate ("Arial")
$hFont = _GDIPlus_FontCreate ($hFamily, 12, 2)
$tLayout = _GDIPlus_RectFCreate (140, 110, 100, 20)
_GDIPlus_GraphicsDrawStringEx ($hGraphic, "Hello world", $hFont, $tLayout, $hFormat, $hBrush)
$savebmp = _GDIPlus_BitmapCreateFromGraphics ($width, $height, $hGraphic)
_GDIPlus_ImageSaveToFile($savebmp, @ScriptDir & "\test.gif")
_GDIPlus_FontDispose ($hFont)
_GDIPlus_FontFamilyDispose ($hFamily)
_GDIPlus_StringFormatDispose ($hFormat)
_GDIPlus_BrushDispose ($hBrush)
_GDIPlus_GraphicsDispose ($hGraphic)
_GDIPlus_Shutdown ()

EDIT: Make sure you modify it to load an image you have on your comp...

Edited by JFee

Regards,Josh

Link to comment
Share on other sites

Basically I am looking to create a GIF file that is transparent with some text on it. I am trying to use GDIPlus functions to accomplish this, but I am almost at a loss of where to start.

First off, is this possible? Essentially I just need to be able to have the BITMAP handle have alpha channels right? Any suggestions are appreciated.

TIA

Here is an example for a PNG file, which is saved with a transparent background. It only shows text and graphics of non background color.

You may be able to adapt it for your purposes.

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)
Opt("PixelCoordMode", 0)

Main()

ShellExecute(@DesktopDir & "\TestWrite.png")
Local $iAns = MsgBox(4, "Exit", '"Yes" to delete Image File & Exit' & @CRLF & '"No"  to exit')
If $iAns = 6 Then FileDelete(@DesktopDir & "\TestWrite.png")


Func Main()
    Local Const $ULW_ALPHA = 2
    Local $hBitmap1, $hBitmap, $hImage2, $hGraphic, $sCLSID
    Local $GuiSizeX = 600, $GuiSizeY = 500
    
    _GDIPlus_Startup()
    $hBitmap1 = _WinAPI_CreateBitmap($GuiSizeX, $GuiSizeY, 1, 32)
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap1)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage2)
    _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFA0)
    
    ;Set pixels
    For $line = 20 To 40
        For $col = 10 To 110
            _GDIPlus_BitmapSetPixel($hImage2, $col, $line, 0xFF00FF00) ; Green
        Next
    Next
    
    ;Draw circle
    Local $hBrushBlue = _GDIPlus_BrushCreateSolid(0xFF0000FF)
    _GDIPlus_GraphicsFillEllipse($hGraphic, 120, 115, 10, 10, $hBrushBlue)
    _GDIPlus_GraphicsDrawEllipse($hGraphic, 120, 115, 10, 10)
    
    ; Text
    Local $hBrush, $hFormat, $hFamily, $hFont, $tLayout
    Local $sString = "Hello world", $aInfo
    Local $hBrushRed = _GDIPlus_BrushCreateSolid(0xFFFF0000)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont   = _GDIPlus_FontCreate($hFamily, 12, 1)
    $tLayout = _GDIPlus_RectFCreate(140, 110, 0, 0)
    $aInfo   = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrushRed)
    
    Local $sString2 = "Hello again"
    $tLayout = _GDIPlus_RectFCreate(140, 135, 0, 0)
    $aInfo   = _GDIPlus_GraphicsMeasureString($hGraphic, $sString2, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString2, $hFont, $aInfo[0], $hFormat, $hBrushBlue)
    
    $hBitmap = ImageMakeTransparentBkGnd($hImage2, $GuiSizeX, $GuiSizeY)
    
    ProgressOff()

    ; Save to file
    $sCLSID = _GDIPlus_EncodersGetCLSID("PNG");"GIF") ;
    _GDIPlus_ImageSaveToFileEx($hBitmap, @DesktopDir & "\TestWrite.png", $sCLSID) ; gif

    ; Clean up resources
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrushRed)
    _GDIPlus_BrushDispose($hBrushBlue)

    _WinAPI_DeleteObject($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hBitmap1)
    _GDIPlus_Shutdown()
    
    Return 1
EndFunc   ;==>Main

Func ImageMakeTransparentBkGnd($hImage2, $GuiSizeX, $GuiSizeY)
    Local $hBitmap1, $Reslt, $width, $height, $stride, $format, $Scan0, $v_Buffer, $v_Value, $iColor
    
    $hBitmap1 = _GDIPlus_BitmapCloneArea($hImage2, 0, 0, $GuiSizeX, $GuiSizeY, $GDIP_PXF32ARGB)
    
    $iColor = GDIPlus_BitmapGetPixel($hBitmap1, 1, 1) ; Transparent color
    
    ProgressOn("Making a color Transparent", "The image is being processed.", "0 percent", -1, -1, 16)

    $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $GuiSizeX, $GuiSizeY, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()
    $width = DllStructGetData ($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    $format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData ($Reslt, "Scan0")

    For $i = 0 To $GuiSizeX - 1
        For $j = 0 To $GuiSizeY - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $v_Value = DllStructGetData($v_Buffer, 1)
            If Hex($v_Value, 6) = Hex($iColor, 6) Then
                DllStructSetData($v_Buffer, 1, Hex($iColor, 6))
            EndIf
        Next
        ProgressSet(Int(100 * $i / ($GuiSizeX)), Int(100 * $i / ($GuiSizeX)) & " percent")
    Next

    _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt)
    GUISetCursor(2)
    Return $hBitmap1
EndFunc   ;==>ImageMakeTransparentBkGnd

;The GetPixel method gets the color of a specified pixel in this bitmap.
Func GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
    Local $tArgb, $pArgb, $aRet
    $tArgb = DllStructCreate("dword Argb")
    $pArgb = DllStructGetPtr($tArgb)
    $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "ptr", $pArgb)
    Return "0x" & Hex(DllStructGetData($tArgb, "Argb"))
EndFunc   ;==>GDIPlus_BitmapGetPixel

Func _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $iArgb)
    Local $aRet
    $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapSetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "dword", $iArgb)
    Return
EndFunc   ;==>_GDIPlus_BitmapSetPixel
Link to comment
Share on other sites

I actually got it working the other day. I wasn't understanding how GDIPlus graphics objects were working, but now that I do I can save it and everything is great. I have a presaved PNG of the desired size, and I load that into an image object. I then use the imagegetgraphicscontext function and add text to it, then save it as another file. Thanks for the help though ;)

Regards,Josh

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...