[API]
ICONDIRENTRY
RtlMoveMemory
Public DestroyIcon

[INCLUDE]
RawIconEntryToIcon
IsHighColor

[Code]
Public Function RawIconToIcon(ByVal xData As String, ByVal DesiredSizeXY As Long) As Long
  Dim xChar1 As String
  Dim xLong1 As Long
  'Dim xLong2 As Long
  Dim xIconCount As Long
  Dim xType1 As ICONDIRENTRY
  'Dim xType2 As BITMAPINFOHEADER
  Dim xBestDPos As Long
  Dim xBestDLen As Long
  Dim xBestBPP As Long

  If Len(xData) > 6 Then
    xChar1 = Left(xData, 4&)
    Call RtlMoveMemory(ByVal VarPtr(xLong1), ByVal xChar1, 4&)
    If xLong1 = &H10000 Then
      xChar1 = Mid(xData, 5&, 2&)
      Call RtlMoveMemory(ByVal VarPtr(xIconCount), ByVal xChar1, 2&)
      If Len(xData) > 6& + xIconCount * 16& Then
        For xLong1 = 0& To xIconCount - 1&
          xChar1 = Mid(xData, 7& + xLong1 * 16&, 16&)
          Call RtlMoveMemory(xType1, ByVal xChar1, 16&)
          If xType1.Width > 0& And xType1.Height > 0& Then
            'Call RawIconEntryToIcon(Mid(xData, xType1.DataPos + 1&, xType1.DataLen), xType1.Width, xType1.Height)
            If xType1.Width = DesiredSizeXY And xType1.Height = DesiredSizeXY Then
              If xType1.BPP_HSY = 0& Then
                Select Case xType1.PalNumCols
                  Case 1&, 2&: xType1.BPP_HSY = 1&
                  Case 3& To 16&: xType1.BPP_HSY = 4&
                  Case 17& To 256&: xType1.BPP_HSY = 8&
                  End Select
                End If
              If xType1.BPP_HSY = 4& And Not IsHighColor Then
                xBestDPos = xType1.DataPos
                xBestDLen = xType1.DataLen
                GoTo LoColorUse
                ElseIf xType1.BPP_HSY > xBestBPP Then
                xBestDPos = xType1.DataPos
                xBestDLen = xType1.DataLen
                xBestBPP = xType1.BPP_HSY
                End If
              End If
            End If
          Next
        If Not xBestBPP = 0& Then
LoColorUse:
'          Stop
          'Call RawBitmapToBitmap(Mid(xData, xBestDPos + 1&, xBestDLen))
          RawIconToIcon = RawIconEntryToIcon(Mid(xData, xBestDPos + 1&, xBestDLen), DesiredSizeXY, DesiredSizeXY)
          'Debug.Print xBestBPP
          End If
        End If
      End If
    End If
  End Function
