Repository: microsoft/PowerToys
File: src/modules/peek/Peek.FilePreviewer/Previewers/ImagePreviewer/ImagePreviewer.cs (C#)

Code before:
            try
            {
                var bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
                var bitmapImage = new BitmapImage();
                using (var stream = new MemoryStream())
                {
                    bitmap.Save(stream, ImageFormat.Bmp);
                    stream.Position = 0;
                    await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
                }

                return bitmapImage;
            }
            finally
            {
                // delete HBitmap to avoid memory leaks
                NativeMethods.DeleteObject(hbitmap);
            }
        }

        public static bool IsFileTypeSupported(string fileExt)
        {
            return _supportedFileTypes.Contains(fileExt.ToLower());
        }

        private static readonly string[] _supportedFileTypes = new string[]
        {
                // Image types
                ".bmp",
                ".gif",
                ".jpg",
                ".jfif",
                ".jfi",
                ".jif",
                ".jpeg",
                ".jpe",
                ".png",
                ".tif",
                ".tiff",
                ".dib",

                // ".heic", // Error in System.Drawing.Image.FromHbitmap(hbitmap);
                ".heif",
                ".hif",
                ".avif",
                ".jxr",
                ".wdp",
                ".ico",
                ".thumb",

                // Raw types

Review comment (performance):
nit: consider making this a hashset for faster lookup times at the cost of memory. @SamChaps am I correct in assuming we want to prioritize performance over memory size?

Code after:
            try
            {
                var bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
                var bitmapImage = new BitmapImage();
                using (var stream = new MemoryStream())
                {
                    bitmap.Save(stream, ImageFormat.Bmp);
                    stream.Position = 0;
                    await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
                }

                return bitmapImage;
            }
            finally
            {
                // delete HBitmap to avoid memory leaks
                NativeMethods.DeleteObject(hbitmap);
            }
        }

        public static bool IsFileTypeSupported(string fileExt)
        {
            return _supportedFileTypes.Contains(fileExt);
        }

        private static readonly HashSet<string> _supportedFileTypes = new HashSet<string>
        {
                // Image types
                ".bmp",
                ".gif",
                ".jpg",
                ".jfif",
                ".jfi",
                ".jif",
                ".jpeg",
                ".jpe",
                ".png",
                ".tif",
                ".tiff",
                ".dib",

                // ".heic", // Error in System.Drawing.Image.FromHbitmap(hbitmap);
                ".heif",
                ".hif",
                ".avif",
                ".jxr",
                ".wdp",
                ".ico",
                ".thumb",

                // Raw types