תחליף את תחילת הקוד, מההצהרה על הDictionary כולל, ועד סוף הפונקציה הראשונה,
בקוד דלהלן:
Public Shared ICO_DIC As New Dictionary(Of String, ImageSource)
'This function get the icon for the specified file path extension
Public Shared Function GetIcon(ByVal filePath As String) As ImageSource
Try
Dim wpfBitmap As ImageSource
'Get the extension
Dim ext As String = IO.Path.GetExtension(filePath)
' בודק אם האיקון כבר קיים
ICO_DIC.TryGetValue(ext, wpfBitmap)
If wpfBitmap IsNot Nothing Then
Return wpfBitmap
End If
Dim shellFileInfo As New SYS_ICO.SHFILEINFO()
SYS_ICO.SHGetFileInfo(ext, 128, shellFileInfo, _
Convert.ToUInt32(Marshal.SizeOf(shellFileInfo)), _
Convert.ToUInt32(256I Or 1I Or 16I))
Dim ico As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shellFileInfo.hIcon)
wpfBitmap = FromIconToImageSource(ico)
ICO_DIC.Add(ext, wpfBitmap)
Return wpfBitmap
Catch ex As Exception
Return FromIconToImageSource(My.Resources.איקון)
End Try
End Function
Private Shared Function FromIconToImageSource(icon As System.Drawing.Icon) As ImageSource
Dim bitmap As System.Drawing.Bitmap = icon.ToBitmap()
Dim hBitmap As IntPtr = bitmap.GetHbitmap()
Dim wpfBitmap As ImageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions())
Return wpfBitmap
End Function
ואח"כ אתה שם את התוצאה במאפיין הImageSource של המחלקה דלעיל.
פורסם במקור בפורום CODE613 ב17/10/2013 19:47 (+03:00)