PidToIconConverter.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Drawing; 7 using System.IO; 8 9 using Microsoft.UI.Xaml.Data; 10 using Microsoft.UI.Xaml.Media.Imaging; 11 12 namespace PowerToys.FileLocksmithUI.Converters 13 { 14 public sealed partial class PidToIconConverter : IValueConverter 15 { 16 public object Convert(object value, Type targetType, object parameter, string language) 17 { 18 var y = PowerToys.FileLocksmithLib.Interop.NativeMethods.PidToFullPath((uint)value); 19 Icon icon = null; 20 21 if (!string.IsNullOrEmpty(y)) 22 { 23 icon = Icon.ExtractAssociatedIcon(y); 24 } 25 26 if (icon != null) 27 { 28 Bitmap bitmap = icon.ToBitmap(); 29 BitmapImage bitmapImage = new BitmapImage(); 30 using (MemoryStream stream = new MemoryStream()) 31 { 32 bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 33 stream.Position = 0; 34 bitmapImage.SetSource(stream.AsRandomAccessStream()); 35 } 36 37 return bitmapImage; 38 } 39 else 40 { 41 return new BitmapImage(); 42 } 43 } 44 45 public object ConvertBack(object value, Type targetType, object parameter, string language) 46 { 47 return value; 48 } 49 } 50 }