Jump to content
Sign in to follow this  
erix

Thumbnail of a file

Recommended Posts

Hi,

Is there anyway to get a thumbnail of a file similar to the way explorer

does.

I'm looking for put the thumbnail of a file in a GUI.

Files are not JPG or BMP or image but PDF or DWG.

Thanks

Share this post


Link to post
Share on other sites

#Include <GDIPlus.au3>
#Include <GUIConstantsEx.au3>
#Include <GUIImageList.au3>
#Include <GUIListView.au3>
#Include <File.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>

Global Const $Path = @WindowsDir & '\Web\Wallpaper'

_GDIPlus_Startup()

$hForm = GUICreate('MyGUI', 800, 600, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX), $WS_EX_COMPOSITED)

$hListView = _GUICtrlListView_Create($hForm, 'Item', 0, 0, 800, 600, BitOR($LVS_AUTOARRANGE, $LVS_ICON))
_GUICtrlListView_SetTextBkColor($hListView, $CLR_NONE)
_GUICtrlListView_SetBkColor($hListView, 0xD9FFEB)
$hImageList = _GUIImageList_Create(64, 64, 5, 1)
_GUICtrlListView_SetImageList($hListView, $hImageList)

$FileList = _FileListToArray($Path, '*', 1)
If IsArray($FileList) Then
    For $i = 1 To $FileList[0]
        _GUICtrlListView_InsertItem($hListView, $FileList[$i], $i - 1)
        If StringRegExpReplace($FileList[$i], '^.*\.', '') = 'ico' Then
            _GUICtrlListView_SetItemIconEx($hListView, $i - 1, $hImageList, $Path & '\' & $FileList[$i])
        Else
            _GUICtrlListView_SetItemImageEx($hListView, $i - 1, $hImageList, $Path & '\' & $FileList[$i])
        EndIf
    Next
EndIf

GUIRegisterMsg($WM_SIZE, 'WM_SIZE')
GUISetState()

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

_GDIPlus_Shutdown()

Func _GUICtrlListView_SetItemIconEx($hWnd, $iIndex, ByRef $hImageList, $sFile, $iIcon = 0)

    Local $Size = _GUIImageList_GetIconSize($hImageList)

    If (Not $Size[0]) Or (Not $Size[1]) Then
        Return 0
    EndIf

    Local $hIcon = DllCall('shell32.dll', 'int', 'SHExtractIconsW', 'wstr', $sFile, 'int', $iIcon, 'int', $Size[0], 'int', $Size[1], 'ptr*', 0, 'ptr*', 0, 'int', 1, 'int', 0)

    If (@error) Or (Not $hIcon[0]) Or (Not $hIcon[5]) Then
        Return 0
    EndIf
    _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon[5])
    _GUICtrlListView_SetItemImage($hWnd, $iIndex, _GUIImageList_GetImageCount($hImageList) - 1)
    _WinAPI_DestroyIcon($hIcon[5])
EndFunc   ;==>_GUICtrlListView_SetItemIconEx

Func _GUICtrlListView_SetItemImageEx($hWnd, $iIndex, ByRef $hImageList, $sFile)

    Local $Size = _GUIImageList_GetIconSize($hImageList)

    If (Not $Size[0]) Or (Not $Size[1]) Then
        Return 0
    EndIf

    Local $W, $H, $hGraphic, $hPic, $hImage, $hIcon

    _GDIPlus_Startup()
    $hPic = _GDIPlus_ImageLoadFromFile($sFile)
    $W = _GDIPlus_ImageGetWidth($hPic)
    $H = _GDIPlus_ImageGetHeight($hPic)
    If ($W < 0) Or ($H < 0) Then
        _GDIPlus_Shutdown()
        Return 0
    EndIf
    If $W < $H Then
        $W = $Size[0] * $W / $H
        $H = $Size[1]
    Else
        $H = $Size[1] * $H / $W
        $W = $Size[0]
    EndIf
    $hImage = DllCall($ghGDIPDll, 'int', 'GdipGetImageThumbnail', 'ptr', $hPic, 'int', $Size[0], 'int', $Size[1], 'ptr*', 0, 'ptr', 0, 'ptr', 0)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage[4])
    _GDIPlus_GraphicsClear($hGraphic, 0)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hPic, ($Size[0] - $W) / 2, ($Size[1] - $H) / 2, $W, $H)
    $hIcon = DllCall($ghGDIPDll, 'int', 'GdipCreateHICONFromBitmap', 'ptr', $hImage[4], 'ptr*', 0)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage[4])
    _GDIPlus_ImageDispose($hPic)
    _GDIPlus_Shutdown()
    If Not $hIcon[2] Then
        Return 0
    EndIf
    _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon[2])
    _GUICtrlListView_SetItemImage($hWnd, $iIndex, _GUIImageList_GetImageCount($hImageList) - 1)
    _WinAPI_DestroyIcon($hIcon[2])
    Return 1
EndFunc   ;==>_GUICtrlListView_SetItemImageEx

Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam)

    Local $Pos

    Switch $hWnd
        Case $hForm
            $Pos = WinGetClientSize($hForm)
            WinMove($hListView, '', 0, 0, $Pos[0], $Pos[1])
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_SIZE


Share this post


Link to post
Share on other sites

Thank you Yashied.

Your script works for image files but not for PDF, ODT, DWG...

Share this post


Link to post
Share on other sites

I found this method who works on Windows XP but not on Windows 7 :

http://www.autoitscript.com/forum/index.php?showtopic=39120

Share this post


Link to post
Share on other sites

I'm curious myself... after some research I came up with this MSDN article:

http://msdn.microsoft.com/en-us/library/aa969353.aspx

IExtractImage or IExtractImage2 are backward compatible to XP, but if you're running Vista or Win7 I guess the IThumbnailProvider interface provides an overall better functionality.

Edit:

Here's a sample for IExtractImage:

http://www.codeproject.com/KB/shell/thumbextract.aspx

Something to tinker on for the COM experts :idea:

Edited by KaFu

Share this post


Link to post
Share on other sites

Well... I can't (at least in reasonable time). But that seems to be the way Windows does it. Maybe someone with an extensive knowledge about the COM interface (which I don't have) becomes curious too and will take a look...

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...