0

I need to extract an icon resource from a file. I have a structure (Resource) that contains the byte array, size, find handle, load handle, lock handle (from FindResource, LoadResource and LockResource, respectively).

I know that I need to obtain the Icon handle of my icon. Then I need to use GetIconInfo to retrieve the bit-mask. Then I use Image.FromHbitmap(h) to get an image. I then use the dimensions of the image as parameters to CreateIconFromResourceEx to retrieve a properly sized Icon.

Does anyone have some code to do this? I can declare all P/Invoke myself. Thanks!

My original code is returning a 32x32 every time.

Dim out As ICONINFO
Dim h As IntPtr = res.hLock
GetIconInfo(h, out)
Dim s As Image = Image.FromHbitmap(out.hbmMask)
h = ResourceExplorer.CreateIconFromResourceEx(res.bArray, res.hSize, True, &H30000, s.Width, s.Height, 0)
PictureBox1.Image = Icon.FromHandle(h).ToBitmap
Rodia
  • 1,407
  • 8
  • 22
  • 29
Adam
  • 77
  • 7
  • Why not using LoadIcon directly then? Or LoadImage if you want more control over the size and other params? There is a sample here: https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.fromhicon(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 – VuVirt Feb 24 '17 at 08:20
  • And some code (C#) to load a resource with LoadImage: http://www.pinvoke.net/default.aspx/user32.loadimage – VuVirt Feb 24 '17 at 08:26
  • yes i saw that. problem is i dont know size of my resource. this is what i need help with (by size i mean dimensions) – Adam Feb 24 '17 at 21:44
  • Icons have predefined sizes, the most common are 16x16 and 32x32. With LoadImage you can load the size you need for your purpose. You can also check the LoadIconWithScaleDown function on Vista and above. Otherwise, if you need to enumerate available icon dimensions in the resource you might check this answer: http://stackoverflow.com/questions/20729156/find-out-number-of-icons-in-an-icon-resource-using-win32-api – VuVirt Feb 25 '17 at 08:15

0 Answers0