Set System.Drawing.Icon as Source for ImageIcon control - WinUI 3

Hemanth B 886 Reputation points
2022-10-06T08:18:43.85+00:00

Hi I am using C# WinUI 3 WinAppSDK for my app.

I have a list of files from my desktop folder for example.
A few are image files which have thumbnails in Windows File Explorer.
I want to have the same thumbnails in my app's ImageIcon or Image element.
I extracted the icon from the files:

      Icon iconn = System.Drawing.Icon.ExtractAssociatedIcon(file.Name);  

Now I want to set this icon for the ImageIcon control. Please tell how

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
889 questions
{count} votes

Accepted answer
  1. Castorix31 90,276 Reputation points
    2022-10-06T09:00:49.17+00:00

    You can use GetThumbnailAsync instead.
    Test with Notepad :

                var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(@"c:\windows\system32\notepad.exe");  
                var iconThumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 32);  
                var bi = new BitmapImage();  
                bi.SetSource(iconThumbnail);  
                imgIcon1.Source = bi;  
    

    248062-imageicon.gif

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.