[python-win32] Extract icon from exe files

Nicolas EISEN neisen at linbox.com
Tue May 5 11:35:14 CEST 2009


Tim Roberts a écrit :
>
> I think you're making this a lot harder than it needs to be.
>
>     import win32ui
>     import win32gui
>     import win32con
>     import win32api
>
>     ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
>     ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)
>
>     large, small =
>     win32gui.ExtractIconEx("c:/windows/system32/shell32.dll",0)
>     win32gui.DestroyIcon(large[0])
>
>     hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) )
>     hbmp = win32ui.CreateBitmap()
>     hbmp.CreateCompatibleBitmap( hdc, ico_x, ico_x )
>     hdc = hdc.CreateCompatibleDC()
>
>     hdc.SelectObject( hbmp )
>     hdc.DrawIcon( (0,0), small[0] )
>     hbmp.SaveBitmapFile( hdc, "save.bmp" )
I make some modification but it's work ! Great !

    import win32ui
    import win32gui
    import win32con
    import win32api
    import cStringIO
    import Image
   
    tempDirectory = os.getenv("temp")
    ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)

    dst = cStringIO.StringIO()

    large, small = win32gui.ExtractIconEx(path,0)
    win32gui.DestroyIcon(small[0])
           
    #creating a destination memory DC
    hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) )
    hbmp = win32ui.CreateBitmap()
    hbmp.CreateCompatibleBitmap(hdc, ico_x, ico_x)
    hdc = hdc.CreateCompatibleDC()
           
    hdc.SelectObject( hbmp )
           
    #draw a icon in it
    hdc.DrawIcon( (0,0), large[0] )
    win32gui.DestroyIcon(large[0])
   
    #convert picture
    hbmp.SaveBitmapFile( hdc, tempDirectory + "\Icontemp.bmp")
   
    im = Image.open(tempDirectory + "\Icontemp.bmp")
    im.save(dst, "JPEG")

    dst.seek(0)
   
    os.remove(tempDirectory + "\Icontemp.bmp")    
    return dst.read()


I have a last little detail : the icon have the tranparence but the bmp 
take this in black. In win32ui, there are not attribute to set the 
background in white before write the bmp in DC ... an idea ?

Thanks a lot.


More information about the python-win32 mailing list