Results 1 to 32 of 32

Thread: Ugly icons...[RESOLVED]

Threaded View

  1. #18

    Thread Starter
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi Pirate.

    I finally got some of it to work. I'm sorry to say that I didn't use your code.

    It turns out that it had been working all the time, except on the icons I had tried. Now I coincidently choose another index and it worked. So it works on some icons and not on others.

    Now, ain't that some weird S**t?

    Now I don't know what to do with the other icons. Perhaps they don't have an alphachannel?

    Here's my code. Maybe you can figure it out?

    VB Code:
    1. Imports System.Runtime.InteropServices
    2. Imports System.Drawing.Imaging
    3.  
    4. Public Class IconExtraction
    5.  
    6. #Region "API"
    7.     <DllImport("shell32.dll", EntryPoint:="ExtractIconEx", CharSet:=CharSet.Auto)> _
    8.     Private Shared Function ExtractIconEx(ByVal Filename As String, ByVal IconIndex As Integer, ByRef hIconLarge As IntPtr, ByRef hIconSmall As IntPtr, ByVal IconCount As Integer) As IntPtr
    9.     End Function
    10.  
    11.     <DllImport("shell32.dll", EntryPoint:="SHGetFileInfo", CharSet:=CharSet.Auto)> _
    12.     Private Shared Function SHGetFileInfo(ByVal FileName As String, ByVal FileAttributes As Integer, ByRef Buffer As SHFileInfo, ByVal FileInfo As Integer, ByVal Flags As Integer) As IntPtr
    13.     End Function
    14.  
    15.     <DllImport("user32", EntryPoint:="GetIconInfo", CharSet:=CharSet.Auto)> _
    16.     Private Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef piconinfo As ICONINFO) As IntPtr
    17.     End Function
    18.  
    19.     <DllImport("Gdi32", EntryPoint:="CreateDC", CharSet:=CharSet.Auto)> _
    20.     Private Shared Function CreateDC(ByVal Driver As String, ByVal DeviceName As String, ByVal Output As String, ByVal Mode As IntPtr) As IntPtr
    21.     End Function
    22.  
    23.     <DllImport("Gdi32", EntryPoint:="GetDeviceCaps", CharSet:=CharSet.Auto)> _
    24.     Private Shared Function GetDeviceCaps(ByVal hDC As IntPtr, ByVal Index As Integer) As Integer
    25.     End Function
    26.  
    27.     <DllImport("Gdi32", EntryPoint:="DeleteDC", CharSet:=CharSet.Auto)> _
    28.     Private Shared Function DeleteDC(ByVal hDC As IntPtr) As Boolean
    29.     End Function
    30.  
    31.     <DllImport("user32", EntryPoint:="DestroyIcon", CharSet:=CharSet.Auto)> _
    32.     Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As IntPtr
    33.     End Function
    34. #End Region
    35.  
    36. #Region "Structures"
    37.     Private Structure SHFileInfo
    38.         Public hIcon As IntPtr
    39.         Public iIcon As Integer
    40.         Public dwAttributes As Integer
    41.         Public szDisplayName As String
    42.         Public szTypeName As String
    43.     End Structure
    44.     Private Structure ICONINFO
    45.         Dim fIcon As Boolean
    46.         Dim xHotspot As Integer
    47.         Dim yHotspot As Integer
    48.         Dim hbmMask As IntPtr
    49.         Dim hbmColor As IntPtr
    50.     End Structure
    51.  
    52. #End Region
    53.  
    54. #Region "Constants"
    55.     Private Const SHGFI_ICON = &H100
    56.     Private Const SHGFI_SMALLICON = &H1   ' Small icon
    57.     Private Const SHGFI_LARGEICON = &H0   ' Large icon
    58.     Private Const SHGFI_USEFILEATTRIBUTES = &H10
    59.  
    60.     Private Const BITSPIXEL As Integer = 12
    61.     Private Const PLANES As Integer = 14
    62. #End Region
    63.  
    64. #Region "Private Functions"
    65.     'Used by the alphafix
    66.     Private Shared Function GetColorDepth() As Integer
    67.         Dim screenDC As IntPtr = CreateDC("DISPLAY", Nothing, Nothing, IntPtr.Zero)
    68.         Dim bitDepth As Integer = GetDeviceCaps(screenDC, BITSPIXEL)
    69.         bitDepth *= GetDeviceCaps(screenDC, PLANES)
    70.         DeleteDC(screenDC)
    71.         Return bitDepth
    72.     End Function
    73.     Private Shared Function FixAlphaBitmap(ByVal bmSource As Bitmap) As Bitmap
    74.  
    75.         'WARNING! This function will fail if the passed bitmap is not of
    76.         'the correct Pixelformat.
    77.         Dim bmData As Imaging.BitmapData
    78.         Dim bmBounds As New Rectangle(0, 0, bmSource.Width, bmSource.Height)
    79.  
    80.         'create a new bitmap with an ARGB format and point it to the same memory
    81.         'location as the original bitmap.
    82.         bmData = bmSource.LockBits(bmBounds, ImageLockMode.ReadOnly, bmSource.PixelFormat)
    83.         Dim dstBitmap As New Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0)
    84.         bmSource.UnlockBits(bmData)
    85.  
    86.         bmData = Nothing
    87.  
    88.         Return dstBitmap
    89.  
    90.     End Function
    91. #End Region
    92.  
    93.     Public Shared Function GetIconCount(ByVal FileName As String) As Integer
    94.         Dim hCount As IntPtr
    95.  
    96.         hCount = ExtractIconEx(FileName, -1, Nothing, Nothing, 1)
    97.  
    98.         Return hCount.ToInt32
    99.     End Function
    100.  
    101.     Public Shared Function ExtractLargeIcon(ByVal FileName As String, ByVal Index As Integer) As Icon
    102.         Dim hIcon As IntPtr
    103.  
    104.         ExtractIconEx(FileName, Index, hIcon, Nothing, 1)
    105.  
    106.         Return Icon.FromHandle(hIcon)
    107.     End Function
    108.     Public Shared Function ExtractSmallIcon(ByVal FileName As String, ByVal Index As Integer) As Icon
    109.         Dim hIcon As IntPtr
    110.  
    111.         ExtractIconEx(FileName, Index, Nothing, hIcon, 1)
    112.  
    113.         Return Icon.FromHandle(hIcon)
    114.     End Function
    115.  
    116.     Public Shared Function ExtractLargeAssociatedIcon(ByVal FileName As String) As Icon
    117.         Dim sh As New SHFileInfo()
    118.  
    119.         SHGetFileInfo(FileName, 0, sh, Marshal.SizeOf(sh), SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_USEFILEATTRIBUTES)
    120.  
    121.         GC.Collect()
    122.  
    123.         Return Icon.FromHandle(sh.hIcon)
    124.     End Function
    125.     Public Shared Function ExtractSmallAssociatedIcon(ByVal FileName As String) As Icon
    126.         Dim sh As New SHFileInfo()
    127.  
    128.         SHGetFileInfo(FileName, 0, sh, Marshal.SizeOf(sh), SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_USEFILEATTRIBUTES)
    129.  
    130.         GC.Collect()
    131.  
    132.         Return Icon.FromHandle(sh.hIcon)
    133.  
    134.     End Function
    135.  
    136.     Public Shared Function ConvertToBitmap(ByVal hIcon As IntPtr) As Bitmap
    137.         Dim B As Bitmap
    138.         Dim hBitmap As IntPtr
    139.         Dim bmp As Bitmap
    140.         Dim ii As New ICONINFO()
    141.  
    142.         GetIconInfo(hIcon, ii)
    143.         hBitmap = ii.hbmColor
    144.         bmp = Bitmap.FromHbitmap(hBitmap)
    145.         If Environment.OSVersion.Version.Major >= 5 AndAlso _
    146.          Environment.OSVersion.Version.Minor >= 1 AndAlso _
    147.          GetColorDepth() = 32 Then
    148.             B = FixAlphaBitmap(bmp).Clone
    149.         Else
    150.             B = Bitmap.FromHicon(hIcon)
    151.         End If
    152.         DestroyIcon(hBitmap)
    153.         DestroyIcon(hIcon)
    154.  
    155.         GC.Collect()
    156.  
    157.         hBitmap = Nothing
    158.         bmp = Nothing
    159.         ii = Nothing
    160.  
    161.         Return B
    162.  
    163.     End Function
    164. End Class
    Last edited by pax; Feb 27th, 2004 at 11:20 AM.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width