def image_to_ICONINFO(img): w, h = img.size from xpra.codecs.argb.argb import rgba_to_bgra #@UnresolvedImport if TRAY_ALPHA and img.mode.find("A")>=0: #ie: RGBA rgb_format = "BGRA" else: rgb_format = "BGR" rgb_data = memoryview_to_bytes(rgba_to_bgra(img.tobytes("raw", rgb_format))) return make_ICONINFO(w, h, rgb_data, rgb_format=rgb_format)
def set_icon_from_data(self, pixels, has_alpha, w, h, rowstride, options={}): #this is convoluted but it works.. log("set_icon_from_data%s", ("%s pixels" % len(pixels), has_alpha, w, h, rowstride, options)) from PIL import Image #@UnresolvedImport if has_alpha: img_format = "RGBA" else: img_format = "RGBX" rgb_format = options.get("rgb_format", "RGBA") img = Image.frombuffer(img_format, (w, h), pixels, "raw", rgb_format, rowstride, 1) assert img, "failed to load image from buffer (%i bytes for %ix%i %s)" % ( len(pixels), w, h, rgb_format) #apparently, we have to use SM_CXSMICON (small icon) and not SM_CXICON (regular size): icon_w = GetSystemMetrics(win32con.SM_CXSMICON) icon_h = GetSystemMetrics(win32con.SM_CYSMICON) if w != icon_w or h != icon_h: log("resizing tray icon to %ix%i", icon_w, icon_h) img = img.resize((w, h), Image.ANTIALIAS) bitmap = 0 mask = 0 try: from xpra.codecs.argb.argb import rgba_to_bgra #@UnresolvedImport bgra = memoryview_to_bytes(rgba_to_bgra(img.tobytes())) bitmap = rgba_to_bitmap(bgra, icon_w, icon_h) mask = CreateBitmap(icon_w, icon_h, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap hicon = CreateIconIndirect(byref(iconinfo)) log("CreateIconIndirect()=%s", hicon) if not hicon: raise ctypes.WinError(ctypes.get_last_error()) except Exception: log.error("Error: failed to set tray icon", exc_info=True) hicon = FALLBACK_ICON finally: if mask: DeleteObject(mask) if bitmap: DeleteObject(bitmap) self.do_set_icon(hicon) UpdateWindow(self.hwnd) self.reset_function = (self.set_icon_from_data, pixels, has_alpha, w, h, rowstride)
def image_to_ICONINFO(img): w, h = img.size from xpra.codecs.argb.argb import rgba_to_bgra #@UnresolvedImport bgra = memoryview_to_bytes(rgba_to_bgra(img.tobytes("raw", "BGRA"))) return make_ICONINFO(w, h, bgra)