WinUI alternative for Wpf's CreateBitmapSourceFromHIcon. #7943
-
Wpf had Imaging.CreateBitmapSourceFromHIcon . How do I do it in WinUI? Basically, I want to show a file's icon in a listview. I plan to use SHGetFileInfo to get HICON. But don't know how to show it in list view. Please help :o) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I think it is simpler to use GetThumbnailAsync, then you can set it as BitmapSource for a control |
Beta Was this translation helpful? Give feedback.
-
You could also do something like that: using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
// Use GDI+ to load our HICON
Bitmap bmp = Icon.FromHandle(hIcon).ToBitmap();
using var ms = new MemoryStream();
// Save it as PNG into RAM
bmp.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
// Create and loud our bitmap source
var bitmapImage = new BitmapImage();
// TODO: Use async variant
bitmapImage.SetSource(ms.AsRandomAccessStream());
// Use GDI+ to load our HBITMAP
Bitmap bmp = Image.FromHbitmap(hBitmap);
using var ms = new MemoryStream();
// Save it as PNG into RAM
bmp.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
// Create and load our bitmap source
var bitmapImage = new BitmapImage();
// TODO: Use async variant
bitmapImage.SetSource(ms.AsRandomAccessStream()); You will need to use NuGet System.Drawing.Common. |
Beta Was this translation helpful? Give feedback.
I think it is simpler to use GetThumbnailAsync, then you can set it as BitmapSource for a control
(like in the test I did in this thread : Set System.Drawing.Icon as Source for ImageIcon control - WinUI 3)