Exemplo n.º 1
0
        private static ImageSource TryGetAppIcon()
        {
            IntPtr iconPtr = IntPtr.Zero;

            try
            {
                var exe = Assembly.GetEntryAssembly().Location;
                int r   = 0;
                // use direct pinvoke to work with unc paths
                iconPtr = Shell32.ExtractAssociatedIcon(IntPtr.Zero, exe, ref r);
                var img = Imaging.CreateBitmapSourceFromHIcon(iconPtr, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                if (img.CanFreeze)
                {
                    img.Freeze();
                }
                return(img);
            }
            catch (Exception ex)
            {
                Trace.TraceError("AppIconImageConverter failed to extract icon: {0}", ex);
            }
            finally
            {
                if (iconPtr != IntPtr.Zero)
                {
                    User32.DestroyIcon(iconPtr);
                }
            }
            return(null);
        }