예제 #1
0
        /// <summary>
        ///     Render the frameworkElement to a "GDI" Icon with the default size.
        /// </summary>
        /// <param name="frameworkElement">FrameworkElement</param>
        /// <param name="size">Optional, specifies the size, if not given the system default is used</param>
        /// <returns>Icon</returns>
        public static Icon ToIcon(this FrameworkElement frameworkElement, int?size = null)
        {
            if (frameworkElement == null)
            {
                throw new ArgumentNullException(nameof(frameworkElement));
            }
            int iconSize = size ?? User32Api.GetSystemMetrics(SystemMetric.SM_CXICON);
            var stream   = new MemoryStream();

            frameworkElement.WriteAsIconToStream(stream, new[] { iconSize });
            // Reset the stream position, otherwise the icon won't be read correctly (pointer is behind the icon)
            stream.Seek(0, SeekOrigin.Begin);
            return(new Icon(stream, iconSize, iconSize));
        }