Files
wehub-resource-sync 79031da543
Spell checking / Report (Push) (push) Blocked by required conditions
Spell checking / Report (PR) (push) Blocked by required conditions
Spell checking / Check Spelling (push) Has been cancelled
Spell checking / Update PR (push) Has been cancelled
Publish Dev Docs Website / build (push) Failing after 1s
Publish Dev Docs Website / deploy (push) Has been skipped
chore: import upstream snapshot with attribution
2026-07-13 12:16:02 +08:00

65 lines
2.3 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Drawing;
using System.IO;
using ManagedCommon;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media.Imaging;
namespace PowerToys.FileLocksmithUI.Converters
{
public sealed partial class PidToIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var y = PowerToys.FileLocksmithLib.Interop.NativeMethods.PidToFullPath((uint)value);
Icon icon = null;
if (!string.IsNullOrEmpty(y))
{
try
{
icon = Icon.ExtractAssociatedIcon(y);
}
catch (Exception ex)
{
// The process image path can be non-empty but no longer exist on disk
// (e.g. self-updating software that deletes its old versioned directory while
// the old process is still running). ExtractAssociatedIcon then throws and,
// because this converter runs per-row during ListView virtualization, the
// exception would otherwise reach App_UnhandledException and fast-fail the app.
// Fall through to the placeholder icon instead of crashing.
Logger.LogWarning($"Couldn't extract the icon for '{y}'. {ex}");
}
}
if (icon != null)
{
Bitmap bitmap = icon.ToBitmap();
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
bitmapImage.SetSource(stream.AsRandomAccessStream());
}
return bitmapImage;
}
else
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value;
}
}
}