Jump to content

[GDI+] How to do a proper error return for GraphicsSetCompositingMode? [Solved]


PAEz
 Share

Recommended Posts

I needed this routine for some stuff Im doing and couldnt find an example on the forums so just wrote a function using what Ive seen others do and it works great but Im not sure on how to do the error return value. Like I said it works, but if Im going to have it in my library Id like it to be right :mellow:

Func _GDIPlus_GraphicsSetCompositingMode($hGraphic,$mode)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetCompositingMode","handle" ,$hGraphic,"int",$mode)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsSetCompositingMode

This function sets the blend mode for graphics image draw commands.

If mode is 0 then the image will be blended according to its alpha.

If mode is 1 then the image will replace what its being drawn on without blending.

Edited by PAEz
Link to comment
Share on other sites

Maybe you can change the return to True or in case of error False

@UEZ I don´t have to much idea but it seems that one mode blends the draw with the background and the other don´t

#include <GDIPlus.au3>

_CreateClearPNG(@DesktopDir & "\MyPNG1.png", false)
_CreateClearPNG(@DesktopDir & "\MyPNG2.png", true)

Func _CreateClearPNG($sFile, $iComposite)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_BitmapCreateFromScan0(400, 400)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    Local $hBrush = _GDIPlus_BrushCreateSolid(0xAA0000FF)
    If $iComposite then _GDIPlus_GraphicsSetCompositingMode($hGraphics, 1)
    _GDIPlus_GraphicsClear($hGraphics, 0xffff0000)
    _gdiplus_graphicsfillellipse($hGraphics,0,0,400,400,$hBrush)
    _GDIPlus_ImageSaveToFile($hImage, $sFile)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>_CreateClearPNG
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Func _GDIPlus_GraphicsSetCompositingMode($hGraphic, $mode)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetCompositingMode", "handle", $hGraphic, "int", $mode)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[0] = true
EndFunc   ;==>_GDIPlus_GraphicsSetCompositingMode
Link to comment
Share on other sites

Thanks monoscout999 Ill check out your file.

And yeah thats what it does monoscout999.

Heres my example (you beat me :mellow:)....

#include <GDIPlus.au3>
_GDIPlus_Startup()
Local $imagespath="C:\Program Files\AutoIt3\Examples\GUI\merlin.gif"
 
Func GdipCreateTexture($hImg, $WrapMode = 0x00)
Local $DLL = "gdiplus.dll"
Local $RESULT
$RESULT = DllCall($DLL, "int", "GdipCreateTexture", "int", $hImg, "int", $WrapMode, "int*", 0)
Return $RESULT[3]
EndFunc   ;==>GdipCreateTexture
 
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
 
Func _GDIPlus_GraphicsSetCompositingMode($hGraphic,$mode)
Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetCompositingMode","handle" ,$hGraphic,"int",$mode)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsSetCompositingMode
 
Local $TextureImage = _GDIPlus_ImageLoadFromFile( $imagespath)
Local $brush = GdipCreateTexture($TextureImage)
Local $image = _GDIPlus_BitmapCreateFromScan0(500, 500)
Local $graphic = _GDIPlus_ImageGetGraphicsContext($image)
 
; Blend mode 0 will blend the pic with the graphic
_GDIPlus_GraphicsClear($graphic, 0x99FFFFFF)
_GDIPlus_GraphicsSetCompositingMode($graphic,0)
_GDIPlus_GraphicsFillRect($graphic, 0, 0, 500, 500, $brush)
_GDIPlus_ImageSaveToFile ($image, @ScriptDir & "\CombineMode0.png")
 
; whilst blend mode 1 will totallly replace the graphic with the image
_GDIPlus_GraphicsSetCompositingMode($graphic,1)
_GDIPlus_GraphicsClear($graphic, 0x99FFFFFF)
_GDIPlus_GraphicsFillRect($graphic, 0, 0, 500, 500, $brush)
_GDIPlus_ImageSaveToFile ($image, @ScriptDir & "\CombineMode1.png")
 
_GDIPlus_BitmapDispose($TextureImage)
_GDIPlus_BitmapDispose($brush)
_GDIPlus_BitmapDispose($image)
_GDIPlus_GraphicsDispose($graphic)
_GDIPlus_Shutdown()
Edited by PAEz
Link to comment
Share on other sites

I edit the example to Splash the images in the screen.

#include <GDIPlus.au3>

_CreatePNG(@DesktopDir & "\MyPNG1.png", false)
_CreatePNG(@DesktopDir & "\MyPNG2.png", true)
_SPLASHPNG(@DesktopDir & "\MyPNG1.png", 350, 50)
_SPLASHPNG(@DesktopDir & "\MyPNG2.png", @desktopwidth - 750, 50)

Func _CreatePNG($sFile, $iComposite)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_BitmapCreateFromScan0(400, 400)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    Local $hBrush = _GDIPlus_BrushCreateSolid(0xAA0000FF)
    If $iComposite then _GDIPlus_GraphicsSetCompositingMode($hGraphics, 1) ; 1 = Not Blend
    _GDIPlus_GraphicsClear($hGraphics, 0xAAff0000)
    _gdiplus_graphicsfillellipse($hGraphics,0,0,400,400,$hBrush)
    _GDIPlus_BrushSetSolidColor($hBrush,0xAA00FFFF)
    _GDIPlus_GraphicsFillRect($hGraphics,0,0,200,200,$hBrush)
    _GDIPlus_ImageSaveToFile($hImage, $sFile)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>_CreateClearPNG
Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Func _GDIPlus_GraphicsSetCompositingMode($hGraphic, $mode)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetCompositingMode", "handle", $hGraphic, "int", $mode)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[0] = true
EndFunc   ;==>_GDIPlus_GraphicsSetCompositingMode
Func _SPLASHPNG($sPNGFile, $iX, $iY, $iWidth = -1, $iHeight = -1)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($sPNGFile)
    If $iWidth = -1 Then $iWidth = _GDIPlus_ImageGetWidth($hImage)
    If $iHeight = -1 Then $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $hDC = _WinAPI_GetDC(0)
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, $iX, $iY, $iWidth, $iHeight)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_ReleaseDC(0, $hDC)
    _GDIPlus_Shutdown()
EndFunc   ;==>_SPLASHPNG
Link to comment
Share on other sites

Thanks for the examples!

_GDIPlus_GraphicsSetCompositingMode() is something like turn on/off alpha channel e.g. from images.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...