Jump to content

How to get icons from this bmp file?


Recommended Posts

  • Developers
1 hour ago, kosamja said:

What function can be used to get icons from this bmp file?

Define "Get". Do you mean to display them in a GUI?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

So, why do you think it will work then as it is simply one single image? 
YOu obviously need to have a properly formatted file for this to work as there is no miracle parameter available. :) 

Jos

 

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

such kind of images (strips) can also be handled by the _GUIImageList* functions. Have a look to this link for an example of use https://www.autoitscript.com/forum/topic/165083-question-on-_guiimagelist-usage/.
In your case posted here, since you want to use it with the TraySetIcon() function, and since that function accepts a filename as parameter, you should convert the frames of the strip at least in single icons, and save them to disk, or, by the workaround from this post (https://www.autoitscript.com/forum/topic/97902-trayseticon-with-handle/) you can manage it by the image handle.

here a quick and dirty example:

#include <GuiImageList.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>

; ----------------------
; https://www.autoitscript.com/forum/topic/97902-trayseticon-with-handle/
Global Const $tagNOTIFYICONDATAW = _
        'dword cbSize;' & _
        'hwnd hWnd;' & _
        'uint uID;' & _
        'uint uFlags;' & _
        'uint uCallbackMessage;' & _
        'hwnd hIcon;' & _
        'wchar szTip[128];' & _
        'dword dwState;' & _
        'dword dwStateMask;' & _
        'wchar szInfo[256];' & _
        'uint uVersion;' & _
        'wchar szInfoTitle[64];' & _
        'dword dwInfoFlags;' ; & $tagGUID

Global $tNID = DllStructCreate($tagNOTIFYICONDATAW), $pNID = DllStructGetPtr($tNID)
Global $hWnd = WinGetHandle(AutoItWinGetTitle()), $hIcon
; ----------------------

Opt("GUIOnEventMode", 1)
HotKeySet("{ESC}", "Quit")

Example()

Func Example()
    _GDIPlus_Startup()
    ; Load Image
    Local $hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & ".\Bitmap351.bmp")
    Local $hGDIBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) ; convert GDIPlus bitmap to GDI format which is usable for _GUIImageList* functions

    ; there are 31 images (frames) in strip
    Local $iFramesInStrip = 31
    Local $iFrameWidth = _GDIPlus_ImageGetWidth($hBitmap) / $iFramesInStrip
    Local $iFrameHeight = _GDIPlus_ImageGetHeight($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap) ; release GDIPlus bitmap because not needed anymore

    $hImages = _GUIImageList_Create($iFrameWidth, $iFrameHeight, 5) ; create image list in 32-bit which supports transparency
    _GUIImageList_Add($hImages, $hGDIBitmap) ; add the whole strip to the ImageList

    ; Create a little GUI
    $hGUI = GUICreate("Demo", 150, 80)

    GUICtrlCreateLabel("see the tray icon...", 10, 10, 140)
    GUICtrlSetFont(-1, 12)
    GUICtrlCreateButton("Quit", 40, 40, 80)
    GUICtrlSetOnEvent(-1, "Quit")
    GUISetState(@SW_SHOW)

    Do
        For $i = 0 To $iFramesInStrip - 1 ; loop all frames

            $hIcon = _GUIImageList_GetIcon($hImages, $i)

            DllStructSetData($tNID, 'cbSize', DllStructGetSize($tNID))
            DllStructSetData($tNID, 'hWnd', $hWnd) ;HWnd($hGUI)) ;
            DllStructSetData($tNID, 'uID', 1)
            DllStructSetData($tNID, 'uFlags', 2)
            DllStructSetData($tNID, 'hIcon', $hIcon)

            _Shell_NotifyIcon(1, $pNID) ; TraySetIcon()

            Sleep(250)

        Next
    Until False
EndFunc   ;==>Example

Func Quit()
    ; free memory
    _GDIPlus_Shutdown()
    _WinAPI_DestroyIcon($hIcon)
    GUIDelete()
    Exit
EndFunc   ;==>Quit

Func _Shell_NotifyIcon($iMessage, $pdata)
    Local $aRet
    $aRet = DllCall('shell32.dll', 'int', 'Shell_NotifyIconW', 'dword', $iMessage, 'ptr', $pdata)
    If @error Then Return SetError(1, 0, 0)
    Return $aRet[0]
EndFunc   ;==>_Shell_NotifyIcon

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Perfect. thanks. Just 1 more question, is it possible to make background color of images transparent instead of black with _GUIImageList_SetBkColor or something else?

aa.jpg.2619892f4b21acec2e2f207dccbf92b5.jpg

 

Edited by kosamja
Link to comment
Share on other sites

 To use transparency, the image must already contain transparent areas, if it does not contain them, it must be modified with a program that can manage transparency and choose the areas that must be transparent * . In short, transparency must already be present in the image.
If you don't have or don't want to install a proper editing program, you can simply use an online image editor.

A quick and simple example:

  1. open this site: http://www.online-image-editor.com/
  2. upload the image to be modified; browse button and upload button (to keep the BMP format check "Convert during upload" and select BMP even if the image is already BMP otherwise it will be saved in PNG)
  3. select the "wizads" tab
  4. choose the "transparency" icon (the diamond)
  5. Click on the background of the image to make it transparent.
  6. save the modified image and use it in the script instead of the previous one.
  7. have fun

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

7 hours ago, Chimp said:

transparency must already be present in the image.

This might be a bit confusing. Images are commonly stored in several layers, for the colors (for example, Red/Green/Blue) and may additionally contain an alpha layer that enodes transparency, from 0 for totally transparent, to 255 for completely opaque. If an image is stored as RGB, an extra alpha layer can be added to make it RGBA, for example.

Free image processing software such as GIMP will show these layers in a panel (on right), allowing you to edit them individually, add/remove/show/hide them, etc. In your case, you would select by color (the black background), mask->add layer mask (pick "selection"), export to a new file, done. Alternatively, you can use Layer->Color to Alpha, allowing you to fiddle more with threshold values for opaqueness.

Edited by RTFC
Link to comment
Share on other sites

I modified @Chimp's example.

 

#include <GuiImageList.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>

; ----------------------
; https://www.autoitscript.com/forum/topic/97902-trayseticon-with-handle/
Global Const $tagNOTIFYICONDATAW = _
        'dword cbSize;' & _
        'hwnd hWnd;' & _
        'uint uID;' & _
        'uint uFlags;' & _
        'uint uCallbackMessage;' & _
        'hwnd hIcon;' & _
        'wchar szTip[128];' & _
        'dword dwState;' & _
        'dword dwStateMask;' & _
        'wchar szInfo[256];' & _
        'uint uVersion;' & _
        'wchar szInfoTitle[64];' & _
        'dword dwInfoFlags;' ; & $tagGUID

Global $tNID = DllStructCreate($tagNOTIFYICONDATAW), $pNID = DllStructGetPtr($tNID)
Global $hWnd = WinGetHandle(AutoItWinGetTitle()), $hIcon
DllStructSetData($tNID, 'cbSize', DllStructGetSize($tNID))
DllStructSetData($tNID, 'hWnd', $hWnd) ;HWnd($hGUI)) ;
DllStructSetData($tNID, 'uID', 1)
DllStructSetData($tNID, 'uFlags', 3)

; ----------------------
Global $hBmp_Anim, $hGfx, $hFrames

Opt("GUIOnEventMode", 1)
HotKeySet("{ESC}", "Quit")

Example()

Func Example()
    _GDIPlus_Startup()
    ; Load Image
    Local $hBitmap_l = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Bitmap351.png")
    Local $iW = _GDIPlus_ImageGetWidth($hBitmap_l), $iH = _GDIPlus_ImageGetHeight($hBitmap_l)
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)

    ;convert black to transparent color - not really needed here as there are not much opaque background pixels visible
    Local $aRemapTable[2][2]
    $aRemapTable[0][0] = 1
    $aRemapTable[1][0] = 0xFF000000 ;black
    $aRemapTable[1][1] = 0          ;transparent
    Local $hIA = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetRemapTable($hIA, $aRemapTable)
    _GDIPlus_GraphicsDrawImageRectRect($hGfx, $hBitmap_l, 0, 0, $iW, $iH, 0, 0, $iW, $iH, $hIA)
    _GDIPlus_ImageDispose($hBitmap_l)
    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_ImageAttributesDispose($hIA)

    ; there are 31 images (frames) in strip
    Local $iFramesInStrip = 31
    Local $iFrameWidth = 18
    Local $iFrameHeight = 32

    $hFrames = _GDIPlus_BitmapCreateFromScan0($iFrameWidth * $iFramesInStrip, $iFrameHeight)
    $hGfx = _GDIPlus_ImageGetGraphicsContext($hFrames)

    Local $sx = 0, $dx = 0
    For $i = 1 To $iFramesInStrip - 3
        _GDIPlus_GraphicsDrawImageRectRect($hGfx, $hBitmap, 20 + $sx, 22, $iFrameWidth, $iFrameHeight, $dx, 0, $iFrameWidth, $iFrameHeight)
        $sx += 32
        $dx += $iFrameWidth
    Next
    ;the last 3 images have a different offset
    $sx -= 8
    For $i = $iFramesInStrip - 2 To $iFramesInStrip
        _GDIPlus_GraphicsDrawImageRectRect($hGfx, $hBitmap, 20 + $sx, 22, $iFrameWidth, $iFrameHeight, $dx, 0, $iFrameWidth, $iFrameHeight)
        $sx += 32
        $dx += $iFrameWidth
    Next
    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_BitmapDispose($hBitmap) ; release GDIPlus bitmap because not needed anymore
    ;_GDIPlus_ImageSaveToFile($hFrames, @ScriptDir & "\TestStripe.png")

    $hBmp_Anim = _GDIPlus_BitmapCreateFromScan0(16, 16)
    $hGfx = _GDIPlus_ImageGetGraphicsContext($hBmp_Anim)

    ; Create a little GUI
    $hGUI = GUICreate("Demo", 150, 80)

    GUICtrlCreateLabel("see the tray icon...", 10, 10, 140)
    GUICtrlSetFont(-1, 12)
    GUICtrlCreateButton("Quit", 40, 40, 80)
    GUICtrlSetOnEvent(-1, "Quit")
    GUISetState(@SW_SHOW)
    $sx = 0

    Do
        For $i = 1 To $iFramesInStrip ; loop all frames

            _GDIPlus_GraphicsClear($hGfx, 0)
            _GDIPlus_GraphicsDrawImageRectRect($hGfx, $hFrames, $sx, 0, $iFrameWidth, $iFrameHeight, 2, 0, 12, 16)
            $sx += $iFrameWidth
            If $sx > $iFrameWidth * ($iFramesInStrip - 1) Then $sx = 0
            $hIcon = _GDIPlus_HICONCreateFromBitmap($hBmp_Anim)
            DllStructSetData($tNID, 'hIcon', $hIcon)
            _Shell_NotifyIcon(1, $pNID) ; TraySetIcon()
            _WinAPI_DestroyIcon($hIcon)
            Sleep(250)

        Next
    Until False
EndFunc   ;==>Example

Func Quit()
    ; free memory
    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_ImageDispose($hBmp_Anim)
    _GDIPlus_BitmapDispose($hFrames)
    _GDIPlus_Shutdown()
    _WinAPI_DestroyIcon($hIcon)
    GUIDelete()
    Exit
EndFunc   ;==>Quit

Func _Shell_NotifyIcon($iMessage, $pdata)
    Local $aRet
    $aRet = DllCall('shell32.dll', 'int', 'Shell_NotifyIconW', 'dword', $iMessage, 'ptr', $pdata)
    If @error Then Return SetError(1, 0, 0)
    Return $aRet[0]
EndFunc   ;==>_Shell_NotifyIcon

The attached image above is in PNG format, anyhow if you have it in BMP format then you should modify the line 40 accordingly, which should also work.

Uncomment line 80 to save new created bitmap.;)

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