Best way to extract .ico from .exe and paint with PyQt?

后端 未结 1 426

I am looking for a way to extract an icon from a .exe file using Python. I know that you can use win32gui\'s ExtractIconEx function to grab the icon of a .exe but this returns a

1条回答
  •  醉梦人生
    2021-02-06 19:05

    There is a method to create QPixmap from a HBITMAP, so the only problem is how to convert HICON to HBITMAP. This can be done using GetIconInfo.

    icons = win32gui.ExtractIconEx('C:/Program Files/Internet Explorer/iexplore.exe', 0, 10)
    info = win32gui.GetIconInfo(icons[0][0])
    pixmap = QtGui.QPixmap.fromWinHBITMAP(info[4])
    info[3].close()
    info[4].close()
    # call win32gui.DestroyIcon on all the icons returned by ExtractIconEx
    

    EDIT: This code will not help with antialiasing and alpha channel. Your new code is almost correct, but you need to tell Qt to load the alpha channel. If you replace:

    self.pixmap = QtGui.QPixmap.fromWinHBITMAP(self.bitmapFromHIcon(large[0]))
    

    with:

    self.pixmap = QtGui.QPixmap.fromWinHBITMAP(self.bitmapFromHIcon(large[0]), 2)
    

    it will do the right thing. The "magic" number 2 should be technically QtGui.QPixmap.Alpha but for some reason Qt doesn't provide the constant.

    0 讨论(0)
提交回复
热议问题