Skip to content

Commit

Permalink
import System after calling clr.AddReference
Browse files Browse the repository at this point in the history
  • Loading branch information
jborbely committed Jul 27, 2021
1 parent 8462dab commit cda10b6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions msl/qt/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ def icon_to_base64(icon, *, fmt='png'):
import ctypes

clr.AddReference('System.Drawing')
import System
from System.Drawing.Imaging import ImageFormat

shell32 = ctypes.windll.shell32
Expand Down Expand Up @@ -488,12 +489,12 @@ def icon_to_base64(icon, *, fmt='png'):
raise OSError('Requested icon {}, the maximum icon index allowed is {}'.format(icon_index, max_index))

# get the icon bitmap and convert it to base64
handle = clr.System.Int32(handle_large.value)
handle_ptr = clr.System.IntPtr.op_Explicit(handle)
bmp = clr.System.Drawing.Bitmap.FromHicon(handle_ptr)
stream = clr.System.IO.MemoryStream()
handle = System.Int32(handle_large.value)
handle_ptr = System.IntPtr.op_Explicit(handle)
bmp = System.Drawing.Bitmap.FromHicon(handle_ptr)
stream = System.IO.MemoryStream()
bmp.Save(stream, img_fmts[fmt])
base = QtCore.QByteArray(clr.System.Convert.ToBase64String(stream.GetBuffer()).encode())
base = QtCore.QByteArray(System.Convert.ToBase64String(stream.GetBuffer()).encode())

# clean up
ctypes.windll.user32.DestroyIcon(handle_large)
Expand Down

0 comments on commit cda10b6

Please sign in to comment.