[python-win32] Extract icon from exe files

Nicolas EISEN neisen at linbox.com
Thu Apr 30 17:11:39 CEST 2009


Tim Roberts a écrit :
> EISEN Nicolas wrote:
>   
>>>  
>>>       
>> I'm lucky, I found ...
>>
>> My Source :
>>
>>   /from win32gui import *
>>   import win32con
>>   from pywintypes import HANDLE
>>    import win32ui 
>>   listHicon = ExtractIconEx("c:\OpenOffice.exe",0)
>>   tupleIcon = GetIconInfo (HANDLE ( listHicon[0][0] ) )
>>
>>   bitmapColor = tupel [4]
>>
>>    picture = win32ui.CreateBitmap()
>>   buffer = picture.GetBitmapBits ( bitmapColor )
>> /
>>
>> Python return :
>> /win32ui.error: GetObject failed on bitmap/
>> for the last line
>>
>> win32ui have CreateBitmap with 0 argument and GetBitmapBits with 1
>> arguments
>> but win32gui have CreateBitmap with 5 arguments and GetBitmapBits have
>> 2 arguments
>>     
>
> Yes.  Can't you see why?  win32gui is just a thin wrapper around the
> actual Win32 APIs.  win32ui is an attempt to turn those APIs into
> something more like Python objects.  Objects have access to additional
> state, so you don't have to specify as many parameters.  When you call
>     picture.GetBitmapBits( bitmapColor )
>
> That's getting the pixels from the bitmap you just created.  The
> parameter it takes is the number of bytes is should copy (so you're
> passing garbage).  Your bitmap doesn't contain anything yet -- you
> haven't even set the size -- so naturally the GetBitmapBits call fails.
>
> You need to do EXACTLY what the demo does.  Create a bitmap, set its
> size, then draw the icon on the bitmap, THEN pull the bits from the
> bitmap.  The icon doesn't actually contain bitmaps.  It contains arrays
> of pixels, but you need them to be a bitmap.
>
>
>   
>> On the demo, hicon is use ton create the variable nid and re use it to
>> display on the screen, but i want get only the bitmap bits.
>>     
>
> I'm not sure why you think these two things are different.  The way you
> get the bitmap bits is to draw the icon on a bitmap.
>
>   

With demo_menu, I script it :

    /from win32gui import */
    /import win32con/
    /from win32api import GetSystemMetrics/


    /ico_x = GetSystemMetrics(win32con.SM_CXSMICON)/
    /ico_y = GetSystemMetrics(win32con.SM_CYSMICON)/

    /large, small = ExtractIconEx("c:\dxdiag.exe",0)/
    /hicon = small[0]/
    /print type(hicon)/
    /DestroyIcon(large[0])/

    /#creating a source memory DC and selecting a icon in it/
    /srcDC = CreateCompatibleDC(0)/
    /SelectObject(srcDC,hicon);/

    /#creating a destination memory DC and selecting a memory bitmap to it/
    /hdcBitmap = CreateCompatibleDC(0)/
    /hdcScreen = GetDC(0)/
    /hbm = CreateCompatibleBitmap(hdcScreen, ico_x, ico_y)/
    /SelectObject(hdcBitmap, hbm)/

    /# copies source DC to memory DC resulting in a copy of hicon in hbm/
    /BitBlt(hdcBitmap,0,0,ico_x,ico_y,srcDC,0,0,win32con.SRCCOPY);/

    /bitmap = CreateBitmapFromHandle(hbm)/
    /hbm.SaveBitmapFile()/

    /DeleteDC(srcDC);/
    /DeleteDC(hdcBitmap);
    /


I suppose, I have my icon picture on the Bitmap /hbm/ but how I get 
file's bits or how I save it ?

There are SaveBitmapFile or GetBitmapBits methods on win32ui (PyCBitmap) 
but I test many disposition, I have always type problem between win32gui 
and win32ui objects.
I try to use win32gui DC with icon and win32ui DC with bitmap, but 
always the type doesn't work : PyHandle (win32gui) is different from 
PyBitmap (win32ui).

Please Help ! I 'm tired ...


More information about the python-win32 mailing list