Jump to content

_GDIPlus_GraphicsDrawImageRectRectTrans()


Malkey
 Share

Recommended Posts

This is a cross between a Monoceres and a Siao script.

The Script

An image file is loaded and placed, (with a slight degree of transparency) on a darkened background. A white spot with a reducing transparent edge sits on top of the darkened background, and, under the transparent image.

Using the up,down, left, and right arrow keys, the white spot is moveable.

The keys D an U decrease or increase the size of the white spot.

The image appears brighter where the spot is.

If the "D" is held down long enough, the white spot starts to get bigger. This was unexpected.

As this is a bit lame, this script is really an example showing off the _GDIPlus_GraphicsDrawImageRectRectTrans() function. May you put it to better use than I did.

#include <GDIPlus.au3>
#include <misc.au3>
Opt("GUIOnEventMode", 1)
$hwnd = GUICreate("GDI+ Sample!", 500, 500)
GUISetOnEvent(-3, "close")
GUISetState()
$sFile = @WindowsDir & "\Web\Wallpaper\Autumn.jpg"
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($sFile)
$hImage2 = _GDIPlus_ImageLoadFromFile(@WindowsDir & "\Web\Wallpaper\Ascent.jpg")
$graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap = _GDIPlus_BitmapCreateFromGraphics(500, 500, $graphics);  This is actually the buffer
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap); This is to access the buffer
$white = _GDIPlus_BrushCreateSolid(0xfFFFFFFF)


$x = 175
$y = 175
$d = 50
draw($x, $y)
Do
    If _IsPressed("25") Then; Left arrow
        $x -= 5
        draw($x, $y)
    EndIf
    If _IsPressed("26") Then; up arrow
        $y -= 5
        draw($x, $y)
    EndIf
    If _IsPressed("27") Then; right arrow
        $x += 5
        draw($x, $y)
    EndIf
    If _IsPressed("28")  Then; down arrow
        $y += 5
        draw($x, $y)
    EndIf
    If _IsPressed("44")Then; D KEY Decrease diameter of circle
        $d -= 1
        draw($x, $y)
    EndIf
    If _IsPressed("55") Then; U KEY increase diameter of circle
        $d += 1
        draw($x, $y)
    EndIf
    Sleep(50)
Until False

Func draw($x, $y)
    _GDIPlus_GraphicsClear($backbuffer, 0xfF808080); Always start with a clean buffer!
    _GDIPlus_GraphicsFillEllipse($backbuffer, $x, $y, $d, $d, $white)
    For $n = 0 To 59
        $Fade = _GDIPlus_PenCreate("0x" & Hex(0xeFFFFFFF - 0x01000000 * 4 * $n), 1, 2)
        _GDIPlus_GraphicsDrawEllipse($backbuffer, $x - $n / 2, $y - $n / 2, $d + $n, $d + $n, $Fade)
        _GDIPlus_PenDispose($Fade)
    Next
    _GDIPlus_GraphicsDrawImageRectRectTrans($backbuffer, $hImage, 0, 0,"","",10,10,480,480,"", 0.7)
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, 500, 500); Present buffer to screen!   
EndFunc   ;==>draw

Func close()
    _GDIPlus_ImageSaveToFile($bitmap, @DesktopDir & "\TestWrite1.png")
    ShellExecute(@DesktopDir & "\TestWrite1.png")
    _GDIPlus_BrushDispose($white)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>close

;
; #FUNCTION# ===================================================================================================
; Name...........: _GDIPlus_GraphicsDrawImageRectRectTrans
; Description ...: Draw an Image object with transparency
; Syntax.........: _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, $iSrcX, $iSrcY, [$iSrcWidth, _
;                                   [$iSrcHeight, [$iDstX, [$iDstY, [$iDstWidth, [$iDstHeight[, [$iUnit = 2]]]]]]])
; Parameters ....: $hGraphics   - Handle to a Graphics object
;                  $hImage      - Handle to an Image object
;                  $iSrcX       - The X coordinate of the upper left corner of the source image
;                  $iSrcY       - The Y coordinate of the upper left corner of the source image
;                  $iSrcWidth   - Width of the source image
;                  $iSrcHeight  - Height of the source image
;                  $iDstX       - The X coordinate of the upper left corner of the destination image
;                  $iDstY       - The Y coordinate of the upper left corner of the destination image
;                  $iDstWidth   - Width of the destination image
;                  $iDstHeight  - Height of the destination image
;                  $iUnit       - Specifies the unit of measure for the image
;                  $nTrans      - Value range from 0 (Zero for invisible) to 1.0 (fully opaque)
; Return values .: Success      - True
;                  Failure      - False
; Author ........: Siao
; Modified.......: Malkey
; Remarks .......:
; Related .......:
; Link ..........; http://www.autoitscript.com/forum/index.php?s=&showtopic=70573&view=findpost&p=517195
; Example .......; Yes
Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _
    $iDstX = "", $iDstY = "", $iDstWidth = "" , $iDstHeight = "", $iUnit = 2, $nTrans = 1)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
    If $iSrcWidth = 0 or $iSrcWidth = "" Then $iSrcWidth = $iW
    If $iSrcHeight = 0 or $iSrcHeight = "" Then $iSrcHeight = $iH
    If $iDstX = "" Then $iDstX = $iSrcX
    If $iDstY = "" Then $iDstY = $iSrcY
    If $iDstWidth = "" Then $iDstWidth = $iSrcWidth
    If $iDstHeight = "" Then $iDstHeight = $iSrcHeight  
    If $iUnit = "" Then $iUnit = 2  
    ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;blending values:
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 2, 1, 2) * DllStructSetData($tColorMatrix, 3, 1, 3) * _
            DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 5)
    ;;create an image attributes object and update its color matrix
    $hImgAttrib =  DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    $hImgAttrib = $hImgAttrib[1]
     DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _
            "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0)    
    ;;draw image into graphic object with alpha blend
    DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
            $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
            $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)             
    ;;clean up
    DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectTrans
Link to comment
Share on other sites

Hi!

The function is really powerful & useful so you should submit it to Gary so it can be included in the next Beta.

Maybe a function that sets the transparency without drawing it too?

Hi monoceres

The only way I could find or think of to set the transparency without drawing it, is the use of a texture brush with a transparency parameter.

Someone might find a use for it.

I went through the submission process before with the best GDIPlus wrapper ever. Since its inclusion, I am the only one who has used it. My thinking now is, that inclusion of mine has added a little bit more to the AutoIt download. If it is on the public forums (my second help file), that's good enough for me.

Here is a repeat of the script in the 1st post, but using the texturebrush with transparency to draw the image.

#include <GDIPlus.au3>
#include <misc.au3>
Opt("GUIOnEventMode", 1)
$hwnd = GUICreate("GDI+ Sample!", 500, 500)
GUISetOnEvent(-3, "close")
GUISetState()
$sFile = @WindowsDir & "\Web\Wallpaper\Autumn.jpg"
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($sFile)
$graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap = _GDIPlus_BitmapCreateFromGraphics(500, 500, $graphics);  This is actually the buffer
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap); This is to access the buffer
$TextureBrush = _GDIPlus_CreateTextureTrans($hImage, 0.4)
$white = _GDIPlus_BrushCreateSolid(0xfFFFFFFF)

$x = 175
$y = 175
$d = 50
draw($x, $y)
Do
    If _IsPressed("25") Then; Left arrow
        $x -= 5
        draw($x, $y)
    EndIf
    If _IsPressed("26") Then; up arrow
        $y -= 5
        draw($x, $y)
    EndIf
    If _IsPressed("27") Then; right arrow
        $x += 5
        draw($x, $y)
    EndIf
    If _IsPressed("28") Then; down arrow
        $y += 5
        draw($x, $y)
    EndIf
    If _IsPressed("44") Then; D KEY Decrease diameter of circle
        $d -= 1
        draw($x, $y)
    EndIf
    If _IsPressed("55") Then; U KEY increase diameter of circle
        $d += 1
        draw($x, $y)
    EndIf
    Sleep(50)
Until False

Func draw($x, $y)
    _GDIPlus_GraphicsClear($backbuffer, 0xfF808080); Always start with a clean buffer!
    _GDIPlus_GraphicsFillEllipse($backbuffer, $x, $y, $d, $d, $white)
    For $n = 0 To 59
        $Fade = _GDIPlus_PenCreate("0x" & Hex(0xeFFFFFFF - 0x01000000 * 4 * $n), 1, 2)
        _GDIPlus_GraphicsDrawEllipse($backbuffer, $x - $n / 2, $y - $n / 2, $d + $n, $d + $n, $Fade)
        _GDIPlus_PenDispose($Fade)
    Next
    _GDIPlus_GraphicsFillRect($backbuffer, 10, 10, 480, 480, $TextureBrush)
    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, 500, 500); Present buffer to screen!
EndFunc   ;==>draw

Func close()
    ;_GDIPlus_ImageSaveToFile($bitmap, @DesktopDir & "\TestWrite1.png")
    ;ShellExecute(@DesktopDir & "\TestWrite1.png")
    _GDIPlus_BrushDispose($white)
    _GDIPlus_BrushDispose($TextureBrush)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>close

;
; #FUNCTION# ============================================================================================
; Name...........: _GDIPlus_CreateTextureTrans
; Description ...: Creates a TextureBrush object based on an image, and sets image transparency property.
; Parameter
;           $hImage  - Pointer to the Image object.
;           $nTrans  - Value range from 0 (Zero for invisible) to 1.0 (fully opaque)
;           $iX      -  Leftmost coordinate of the image portion to be used by this brush.
;           $iY      - Uppermost coordinate of the image portion to be used by this brush.
;           $iWidth  - Width of the brush and width of the image portion to be used by the brush.
;           $iHeight - Height of the brush and height of the image portion to be used by the brush.
; Return   - Handle (pointer) to the new created TextureBrush object.
Func _GDIPlus_CreateTextureTrans($hImage, $nTrans = 0.5, $iX = 0, $iY = 0, $iWidth = "", $iHeight = "") 
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
    If $iWidth = 0 Or $iWidth = "" Then $iWidth = $iW
    If $iHeight = 0 Or $iHeight = "" Then $iHeight = $iH
    ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;blending values:
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 2, 1, 2) * DllStructSetData($tColorMatrix, 3, 1, 3) * _
            DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 5)
    ;;create an image attributes object and update its color matrix
    $hImgAttrib = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    $hImgAttrib = $hImgAttrib[1]
    DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _
            "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0)
    ;;draw image into graphic object with alpha blend
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateTextureIAI", "hwnd", $hImage, "ptr", $hImgAttrib, "int", $iX, "int", _
            $iY, "int", $iWidth, "int", $iHeight, "ptr*", 0)
    ;clean up
    DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return $aResult[7]
EndFunc   ;==>_GDIPlus_CreateTextureTrans
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...