Skalieren und SetBitmap

  • Nabend Leute,

    ich nutze SetBitmap um ein PNG auf eine ChildGui zu setzen. Nun hab ich die Funktion _ZoomOut geschrieben, welches halt das Image verkleinert und transparent macht. Wenn es nun verkleinert wird, wird es ja nach links oben verkleinert natürlich. Was muss ich machen, damit es zur Mitte verkleinert wird?

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <WinAPI.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)
    Opt("MouseCoordMode", 2)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Global Const $AC_SRC_ALPHA = 1

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGui = GUICreate("Visu", 400, 400)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUICtrlCreateButton("Zoom Out", 20, 20, 80, 25)
    GUICtrlSetOnEvent(-1, "_ZoomOut")
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    Global $hImage = _GDIPlus_ImageLoadFromFile("Rect.png")
    $hImage = Scale_Image($hImage, _GDIPlus_ImageGetWidth($hImage), _GDIPlus_ImageGetHeight($hImage))
    Global $hGuiChild = GUICreate("", _GDIPlus_ImageGetWidth($hImage), _GDIPlus_ImageGetHeight($hImage), 100, 100, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $hGui)
    SetBitmap($hGuiChild, $hImage, 255)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hImage)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _ZoomOut()
    Local $_hImage = _GDIPlus_ImageLoadFromFile("Rect.png")
    Local $_fScale = 1.0
    Local $_aWGP = WinGetPos($hGuiChild)
    For $i = 250 To 0 Step -10
    $_fScale -= 0.02
    $_hImageNew = Scale_Image($_hImage, _GDIPlus_ImageGetWidth($_hImage) * $_fScale, _GDIPlus_ImageGetHeight($_hImage) * $_fScale)
    SetBitmap($hGuiChild, $_hImageNew, $i)
    Sleep(20)
    Next
    _GDIPlus_ImageDispose($_hImage)
    _WinAPI_DeleteObject($_hImage)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func Scale_Image($hImage, $newW, $newH)
    Local $newImage = _GDIPlus_BitmapCreateFromScan0($newW, $newH)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($newImage)
    _GDIPlus_GraphicsSetInterpolationMode($hContext, 7)
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $newW, $newH)
    _GDIPlus_GraphicsDispose($hContext)
    Return SetError(0, 0, $newImage)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_GraphicsSetInterpolationMode($hGraphics, $iInterpolationMode)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "hwnd", $hGraphics, "int", $iInterpolationMode)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
    EndFunc ;==>_GDIPlus_GraphicsSetInterpolationMode

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    [/autoit] [autoit][/autoit] [autoit]

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", $AC_SRC_ALPHA)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
    EndFunc ;==>SetBitmap

    [/autoit]
  • Hi,

    wenn du _ZoomOut() und Scale_Image folgendermaßen abänderst, müsste es gehen:

    Spoiler anzeigen
    [autoit]

    Func _ZoomOut()
    Local $_hImage = _GDIPlus_ImageLoadFromFile("Rect.png")
    Local $_fScale = 1.0
    Local $_aWGP = WinGetPos($hGuiChild)
    Local $_iImageW = _GDIPlus_ImageGetWidth($_hImage)
    Local $_iImageH = _GDIPlus_ImageGetHeight($_hImage)
    For $i = 250 To 0 Step -10
    $_fScale -= 0.02
    $_hImageNew = Scale_Image($_hImage, _GDIPlus_ImageGetWidth($_hImage) * $_fScale, _GDIPlus_ImageGetHeight($_hImage) * $_fScale, True, $_iImageW, $_iImageH)
    SetBitmap($hGuiChild, $_hImageNew, $i)
    Sleep(20)
    Next
    _GDIPlus_ImageDispose($_hImage)
    _WinAPI_DeleteObject($_hImage)
    EndFunc ;==>_ZoomOut

    [/autoit] [autoit][/autoit] [autoit]

    Func Scale_Image($hImage, $newW, $newH, $fCenter = False, $oldW = 0, $oldH = 0)
    Local $newImage = _GDIPlus_BitmapCreateFromScan0($newW, $newH)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($newImage)
    _GDIPlus_GraphicsSetInterpolationMode($hContext, 7)
    If $fCenter Then
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, $oldW - $newW, $oldH - $newH, $newW, $newH)
    Else
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $newW, $newH)
    EndIf
    _GDIPlus_GraphicsDispose($hContext)
    Return SetError(0, 0, $newImage)
    EndFunc ;==>Scale_Image

    [/autoit]

    mfg
    Developer30

    "Je mehr Käse, desto mehr Löcher; je mehr Löcher, desto weniger Käse. Ergo: Je mehr Käse, desto weniger Käse. 8| "
    "Programmers never die: they just GOSUB without RETURN"
    "I tried to change the world but I couldn't find the source code."

  • Probiere es mal damit:

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <WinAPI.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)
    Opt("MouseCoordMode", 2)

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $AC_SRC_ALPHA = 1

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGui = GUICreate("Visu", 400, 400)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUICtrlCreateButton("Zoom Out", 20, 20, 80, 25)
    GUICtrlSetOnEvent(-1, "_ZoomOut")
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    Global $hImage = _GDIPlus_ImageLoadFromFile("Rect.png")
    Global $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
    $hImage = Scale_Image($hImage, $iW, $iH)
    Global $iPosX = 100, $iPosY = 100
    Global $hGuiChild = GUICreate("", $iW, $iH, $iPosX, $iPosY, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $hGui)
    SetBitmap($hGuiChild, $hImage, 255)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hImage)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    Global $fDeltaX = _WinAPI_GetSystemMetrics(7), $fDeltaY = _WinAPI_GetSystemMetrics(8) + _WinAPI_GetSystemMetrics(4)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _ZoomOut()
    Local $_hImage = _GDIPlus_ImageLoadFromFile("Rect.png")
    Local $_fScale = 1.0
    Local $newW, $newH, $aPos, $aSize
    For $i = 250 To 0 Step -10
    $_fScale -= 0.02
    $newW = _GDIPlus_ImageGetWidth($_hImage) * $_fScale
    $newH = _GDIPlus_ImageGetHeight($_hImage) * $_fScale
    $_hImageNew = Scale_Image($_hImage, $newW, $newH)
    SetBitmap($hGuiChild, $_hImageNew, $i)
    $aPos = WinGetPos($hGui)
    WinMove($hGuiChild, "", $iPosX + $fDeltaX + $aPos[0] + ($iW - $newW) / 2, $iPosY + $fDeltaY + $aPos[1] + ($iH - $newH) / 2)
    Sleep(20)
    Next
    _GDIPlus_ImageDispose($_hImage)
    _WinAPI_DeleteObject($_hImage)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func Scale_Image($hImage, $newW, $newH)
    Local $newImage = _GDIPlus_BitmapCreateFromScan0($newW, $newH)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($newImage)
    _GDIPlus_GraphicsSetInterpolationMode($hContext, 7)
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $newW, $newH)
    _GDIPlus_GraphicsDispose($hContext)
    Return SetError(0, 0, $newImage)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    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

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_GraphicsSetInterpolationMode($hGraphics, $iInterpolationMode)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "hwnd", $hGraphics, "int", $iInterpolationMode)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
    EndFunc ;==>_GDIPlus_GraphicsSetInterpolationMode

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    [/autoit] [autoit][/autoit] [autoit]

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", $AC_SRC_ALPHA)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
    EndFunc ;==>SetBitmap

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    Einmal editiert, zuletzt von UEZ (12. Oktober 2012 um 09:50)