Jump to content

Setting PNG file to an image pic Control?


nobbe
 Share

Recommended Posts

hi

so far im setting an PNG to the control with this workaround a direct loading like the gif / jpg wont work??

$image_pic = GUICtrlCreatePic("", 16, 424, 305, 265)


Switch $type
        Case "JPG"
            GUICtrlSetImage($image_pic, $path)
        Case "GIF"
            GUICtrlSetImage($image_pic, $path)
        Case "PNG"
            _GDIPlus_Startup()
            Local $hImage
            $hImage = _GDIPlus_ImageLoadFromFile($path)
            _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\temp.jpg")
            _GDIPlus_ImageDispose($hImage)
            _GDIPlus_Shutdown()

            ; now set the Converted pic to the image space
            GUICtrlSetImage($image_pic, @ScriptDir & "\temp.jpg")
Link to comment
Share on other sites

Hi! Try this:

#include <GuiConstants.au3>
#include <GDIPlus.au3>

$hGui = GUICreate("Test GUI", 300, 200)

$hMenu = GUICtrlCreateMenu("&File")

$OpenItem = GUICtrlCreateMenuItem("&Open", $hMenu)

$hPic = GUICtrlCreatePic("", 0, 0, 300, 180)
GUICtrlSetState(-1, $GUI_DISABLE)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $OpenItem
            $OpenFile = FileOpenDialog("Select image file", "", "Img files (*.jpg;*.gif;*.png)")
            If Not @error Then _SetBackImage($OpenFile)
    EndSwitch   
WEnd

Func _SetBackImage($sFile)
    Local $iExt = StringRegExpReplace($sFile, "^.*\.", "")
    
    Switch $iExt
        Case "jpg", "gif"
            GUICtrlSetImage($hPic, $sFile)
        Case "png"
            _GDIPlus_Startup()
            Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
            Local $hCLSID = _GDIPlus_EncodersGetCLSID("BMP")
            _GDIPlus_ImageSaveToFileEx($hImage, @ScriptDir & "\test.bmp", $hCLSID)
            GUICtrlSetImage($hPic, @ScriptDir & "\test.bmp")
            _GDIPlus_ImageDispose($hImage)
            _GDIPlus_Shutdown()
    EndSwitch
EndFunc
:D
Link to comment
Share on other sites

Try this:

$image_pic = GUICtrlCreatePic("", 16, 424, 305, 265)
Switch $type
        Case "JPG"
            GUICtrlSetImage($image_pic, $path)
        Case "GIF"
            GUICtrlSetImage($image_pic, $path)
        Case "PNG"
            _GDIPlus_Startup()
            Local $hImage
            $hImage = _GDIPlus_ImageLoadFromFile($path)
            SetBitmap($image_pic, $hImage)
            _GDIPlus_ImageDispose($hImage)
            _GDIPlus_Shutdown()
EndSwitch
....

Func SetBitmap($hGUI, $hImage)
Const $AC_SRC_ALPHA = 1
Const $ULW_ALPHA = 2
Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
    $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", 100)
    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
Link to comment
Share on other sites

thanks but doesnt work :-(

_WinAPI_UpdateLayeredWindow seems not to update the single picture control??

You are right. I had it working on a GUI not an actual Pic on the GUI.

Can't revert right now, I will test and revert later on. Perhaps someone else can provide the solution or run-with this.

Link to comment
Share on other sites

#include <GuiConstantsEX.au3>
#include <GDIPlus.au3>

Global $hGui, $iExt
Global $hPic, $hImage, $hGraphic
Global $GDI = ""
Global $iW = 300, $iH = 200

;Global $Border[4] = [10, 10, ($iW-20), ($iH-20)] ; 10 pixel border
Global $Border[4] = [0, 0, $iW, $iH] ; no border

$hGui = GUICreate("Test GUI", $iW, $iH)
$hPic = GUICtrlCreatePic("", $border[0], $border[1], $border[2], $border[3])
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()

$OpenFile = FileOpenDialog("Select image file", "", "Img files (*.jpg;*.gif;*.png)")
If Not @error Then
    _SetBackImage($OpenFile)
Else
    Exit
EndIf

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch   
WEnd

; Release resources
If $iExt = "png" Then
    Beep(1000,5)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndIf

Func _SetBackImage($sFile)
    $iExt = StringRegExpReplace($sFile, "^.*\.", "")

    Switch $iExt
        Case "jpg", "gif"
            GUICtrlSetImage($hPic, $sFile)
        Case "png"
            _GDIPlus_Startup()
            $hImage = _GDIPlus_ImageLoadFromFile($sFile)
            $hGraphic = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($hPic))
            _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage, 0, 0, $border[2], $border[3])
    EndSwitch
EndFunc

I see fascists...

Link to comment
Share on other sites

This is what i'm using for my script,it has an extra feature of keeping aspectratio,but that's optional.But don't forget you have to get the control size by yourself.

FUnc _GUICtrlStatic_SetPicture($Control,Const $File,Const $SizeX,Const $SizeY,Const $KeepRatio = False)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
        
    IF NOT FileExists($File) Then Return SetError(1)
    
    _GDIPlus_Startup()
    Local $hResizedBitmap = _CreateBitmap($SizeX, $SizeY)
    Local $hResizedImage = _GDIPlus_BitmapCreateFromHBITMAP($hResizedBitmap)
    
    Local $hOrigImage = _GDIPlus_ImageLoadFromFile($File)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hResizedImage)
    
    Local $DestX = 0
    Local $DestY = 0
    Local $FSizeX = $SizeX
    Local $FSizeY = $SizeY

    IF $KeepRatio Then
        Local $iWidth = _GDIPlus_ImageGetWidth($hOrigImage)
        Local $iHeight = _GDIPlus_ImageGetHeight($hOrigImage)
        Local $picRatio = $iWidth / $iHeight
        
        IF $picRatio >= 1 Then
            $FsizeY = $FsizeX / $picRatio
            If $FsizeY > $SizeY Then
                $FSizeY = $SizeY
                $FSizeX = $FSizeY * $picRatio
            EndIf
        EndIF
        IF $picRatio <= 1 Then
            $FsizeX = $FsizeY * $picRatio
            If $FsizeX > $SizeX Then
                $FSizeX = $SizeX
                $FSizeY = $FSizeX / $picRatio
            EndIf
        EndIF
        
        $DestX = ($SizeX - $FSizeX) / 2
        $DestY = ($SIzeY - $FSizeY) / 2
    EndIF
    
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hOrigImage, $DestX,$DestY,$FSizeX, $FSizeY) 
    
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hResizedImage,0x00000000)
    If NOT IsHwnd($Control) Then $Control = GUICtrlGetHandle($Control)
    
    $oldBitmap = _SendMessage($Control,$STM_SETIMAGE,$IMAGE_BITMAP,$hBitmap,0,"wparam","lparam","hwnd")
    IF $oldBitmap <> 0 Then
        _WinAPI_DeleteObject($oldBitmap)
    EndIf
    
    _GDIPlus_ImageDispose($hOrigImage)
    _GDIPlus_ImageDispose($hResizedImage)
    _GDIPlus_GraphicsDispose($hGraphics)
    _WinAPI_DeleteObject($hResizedBitmap)
    
    _GDIPlus_Shutdown()
EndFUnc

Func _CreateBitmap(Const $iW,Const $iH)
    Local $hWnd, $hDDC, $hCDC, $hBMP
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    Return $hBMP
EndFunc   ;==>_CreateBitmap
Edited by danielkza
Link to comment
Share on other sites

rover

What the function _GDIPLus_GraphicsDrawImageRect? From latest beta version?

Yes,i forgot about that.Try it now:

FUnc _GUICtrlStatic_SetPicture($Control,Const $File,Const $SizeX,Const $SizeY,Const $KeepRatio = False)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
        
    IF NOT FileExists($File) Then Return SetError(1)
    
    _GDIPlus_Startup()
    Local $hResizedBitmap = _CreateBitmap($SizeX, $SizeY)
    Local $hResizedImage = _GDIPlus_BitmapCreateFromHBITMAP($hResizedBitmap)
    
    Local $hOrigImage = _GDIPlus_ImageLoadFromFile($File)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hResizedImage)
    
    Local $DestX = 0
    Local $DestY = 0
    Local $FSizeX = $SizeX
    Local $FSizeY = $SizeY

    Local $iWidth = _GDIPlus_ImageGetWidth($hOrigImage)
    Local $iHeight = _GDIPlus_ImageGetHeight($hOrigImage)

    IF $KeepRatio Then

        Local $picRatio = $iWidth / $iHeight
        
        IF $picRatio >= 1 Then
            $FsizeY = $FsizeX / $picRatio
            If $FsizeY > $SizeY Then
                $FSizeY = $SizeY
                $FSizeX = $FSizeY * $picRatio
            EndIf
        EndIF
        IF $picRatio <= 1 Then
            $FsizeX = $FsizeY * $picRatio
            If $FsizeX > $SizeX Then
                $FSizeX = $SizeX
                $FSizeY = $FSizeX / $picRatio
            EndIf
        EndIF
        
        $DestX = ($SizeX - $FSizeX) / 2
        $DestY = ($SIzeY - $FSizeY) / 2
    EndIF
    
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hOrigImage, 0,0,$iWidth,$iHeight,$DestX,$DestY,$FSizeX, $FSizeY) 
    
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hResizedImage,0x00000000)
    If NOT IsHwnd($Control) Then $Control = GUICtrlGetHandle($Control)
    
    $oldBitmap = _SendMessage($Control,$STM_SETIMAGE,$IMAGE_BITMAP,$hBitmap,0,"wparam","lparam","hwnd")
    IF $oldBitmap <> 0 Then
        _WinAPI_DeleteObject($oldBitmap)
    EndIf
    
    _GDIPlus_ImageDispose($hOrigImage)
    _GDIPlus_ImageDispose($hResizedImage)
    _GDIPlus_GraphicsDispose($hGraphics)
    _WinAPI_DeleteObject($hResizedBitmap)
    
    _GDIPlus_Shutdown()
EndFUnc

Func _CreateBitmap(Const $iW,Const $iH)
    Local $hWnd, $hDDC, $hCDC, $hBMP
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    Return $hBMP
EndFunc   ;==>_CreateBitmap
Edited by danielkza
Link to comment
Share on other sites

Png with alpha channel to pic crtl

needs prospeed.dll and udf.

#include <GUIConstants.au3>
#include <prospeed.au3>

HotKeySet("{Esc}","_exit")
Opt("GUIOnEventMode", 1)

$gui = GUICreate("Png to Ctrl",600,200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$pic = GUICtrlCreatePic("",50,50,500,100)
GUISetState()

$ctrlDC = CtrlGetDC($pic)

$png = ImportPng(0,"LaunchySkin.png")
PaintPng(0,$ctrlDC,0,0,$png,0)

While 1
    Sleep(1000)
WEnd
    
Func _exit()
    FreePng(0,$png)
    Exit
EndFunc

post-5777-1209240357_thumb.png

Link to comment
Share on other sites

Try this:

$image_pic = GUICtrlCreatePic("", 16, 424, 305, 265)
Switch $type
        Case "JPG"
            GUICtrlSetImage($image_pic, $path)
        Case "GIF"
            GUICtrlSetImage($image_pic, $path)
        Case "PNG"
            _GDIPlus_Startup()
            Local $hImage
            $hImage = _GDIPlus_ImageLoadFromFile($path)
            SetBitmap($image_pic, $hImage)
            _GDIPlus_ImageDispose($hImage)
            _GDIPlus_Shutdown()
EndSwitch
....

Func SetBitmap($hGUI, $hImage)
Const $AC_SRC_ALPHA = 1
Const $ULW_ALPHA = 2
Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
    $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", 100)
    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

This script probably does not have an effect

Link to comment
Share on other sites

@ taotao878

it was only a raw example without buffering the screen

this one has screenbuffers

you can minimize or put other windows ontop of it.

#include <GUIConstants.au3>
#include <prospeed.au3>

HotKeySet("{Esc}","_exit")
Opt("GUIOnEventMode", 1)

$gui = GUICreate("Png to Ctrl",600,200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$pic = GUICtrlCreatePic("",50,50,500,100)
GUISetState()

$hdc = GetHDC()
$ctrlDC = CtrlGetDC($pic)

$png = ImportPng(0,"LaunchySkin.png")
PaintPng(0,$ctrlDC,0,0,$png,0)

CreateBuffer(600,200)
SetBuffer($hdc)

While 1
    Sleep(1000)
WEnd
    
Func _exit()
    FreePng(0,$png)
    Exit
EndFunc
Edited by jpam
Link to comment
Share on other sites

@ taotao878

it was only a raw example without buffering the screen

this one has screenbuffers

you can minimize or put other windows ontop of it.

#include <GUIConstants.au3>
#include <prospeed.au3>

HotKeySet("{Esc}","_exit")
Opt("GUIOnEventMode", 1)

$gui = GUICreate("Png to Ctrl",600,200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$pic = GUICtrlCreatePic("",50,50,500,100)
GUISetState()

$hdc = GetHDC()
$ctrlDC = CtrlGetDC($pic)

$png = ImportPng(0,"LaunchySkin.png")
PaintPng(0,$ctrlDC,0,0,$png,0)

CreateBuffer(600,200)
SetBuffer($hdc)

While 1
    Sleep(1000)
WEnd
    
Func _exit()
    FreePng(0,$png)
    Exit
EndFunc
Thanks jpam.

I thought that it should also be possible to realize with API and GDI+. but i cann't do it.

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