Ejemplo n.º 1
0
        private static byte[] ToMetafile(this Identicon icon)
        {
            using (var desktopGraphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                var hdc = desktopGraphics.GetHdc();
                try
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var img = new Metafile(memoryStream, hdc,
                                                      new System.Drawing.Rectangle(0, 0, icon.Size, icon.Size), MetafileFrameUnit.Pixel,
                                                      EmfType.EmfPlusDual))
                        {
                            using (var graphics = Graphics.FromImage(img))
                            {
                                icon.Draw(graphics);
                            }
                        }

                        return(memoryStream.ToArray());
                    }
                }
                finally
                {
                    desktopGraphics.ReleaseHdc(hdc);
                }
            }
        }
Ejemplo n.º 2
0
        private static string GenerateSvg(this Identicon icon, bool fragment)
        {
            var renderer = new SvgRenderer(icon.Size, icon.Size);

            icon.Draw(renderer);
            return(renderer.ToSvg(fragment));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Renders an <see cref="Identicon"/> as a WPF <see cref="Visual"/>.
        /// </summary>
        /// <param name="icon">The identicon to convert to a <see cref="Visual"/>.</param>
        public static Visual ToVisual(this Identicon icon)
        {
            var visual = new DrawingVisual();

            using (var context = visual.RenderOpen())
            {
                icon.Draw(context);
            }

            return(visual);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> as an Enhanced Metafile (EMF).
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="stream">The stream to which the EMF data will be written.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> was <c>null</c>.</exception>
        public static void SaveAsEmf(this Identicon icon, Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var emf = icon.ToMetafile();

            stream.Write(emf, 0, emf.Length);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> as an Enhanced Metafile (EMF) asynchronously.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="stream">The stream to which the EMF data will be written.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> was <c>null</c>.</exception>
        public static Task SaveAsEmfAsync(this Identicon icon, Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var emf = icon.ToMetafile();

            return(stream.WriteAsync(emf, 0, emf.Length));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file asynchronously.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="stream">The stream to which the SVG data will be written.</param>
        /// <param name="fragment">
        /// If <c>true</c> the generated SVG will not be encapsulated in the root svg element making
        /// it suitable to be embedded in another SVG.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> was <c>null</c>.</exception>
        public static Task SaveAsSvgAsync(this Identicon icon, Stream stream, bool fragment)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var binarySvg = icon.GenerateBinarySvg(fragment);

            return(stream.WriteAsync(binarySvg, 0, binarySvg.Length));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file asynchronously.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to which the SVG data will be written.</param>
        /// <param name="fragment">
        /// If <c>true</c> the generated SVG will not be encapsulated in the root svg element making
        /// it suitable to be embedded in another SVG.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="writer"/> was <c>null</c>.</exception>
        public static Task SaveAsSvgAsync(this Identicon icon, TextWriter writer, bool fragment)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var svg = icon.GenerateSvg(fragment);

            return(writer.WriteAsync(svg));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Portable Network Graphics (PNG) file asynchronously.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="stream">The stream to which the PNG data will be written.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> was <c>null</c>.</exception>
        public static Task SaveAsPngAsync(this Identicon icon, Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var pngData = icon.GeneratePng();

            return(stream.WriteAsync(pngData, 0, pngData.Length));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Portable Network Graphics (PNG) file.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="stream">The stream to which the PNG data will be written.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> was <c>null</c>.</exception>
        public static void SaveAsPng(this Identicon icon, Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var pngData = icon.GeneratePng();

            stream.Write(pngData, 0, pngData.Length);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to which the SVG data will be written.</param>
        /// <param name="fragment">
        /// If <c>true</c> the generated SVG will not be encapsulated in the root svg element making
        /// it suitable to be embedded in another SVG.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="writer"/> was <c>null</c>.</exception>
        public static void SaveAsSvg(this Identicon icon, TextWriter writer, bool fragment)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var svg = icon.GenerateSvg(fragment);

            writer.Write(svg);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Renders an <see cref="Identicon"/> as a WPF <see cref="Visual"/>.
        /// </summary>
        /// <param name="icon">The identicon to convert to a <see cref="Visual"/>.</param>
        public static Visual ToVisual(this Identicon icon)
        {
            var visual = new DrawingVisual();

            using (var context = visual.RenderOpen())
            {
                var iconBounds = icon.GetIconBounds();
                icon.Draw(context, iconBounds);
            }

            return(visual);
        }
Ejemplo n.º 12
0
        private static byte[] GeneratePng(this Identicon icon)
        {
            var renderer = new PngRenderer(icon.Size, icon.Size);

            icon.Draw(renderer);

            using (var memoryStream = new MemoryStream())
            {
                renderer.SavePng(memoryStream);
                return(memoryStream.ToArray());
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file asynchronously.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="path">The path to the SVG file to create. If the file already exists it will be overwritten.</param>
        /// <param name="fragment">
        /// If <c>true</c> the generated SVG will not be encapsulated in the root svg element making
        /// it suitable to be embedded in another SVG.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> was <c>null</c>.</exception>
        public static async Task SaveAsSvgAsync(this Identicon icon, string path, bool fragment)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                await icon.SaveAsSvgAsync(stream, fragment);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Portable Network Graphics (PNG) file.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="path">The path to the PNG file to create. If the file already exists it will be overwritten.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> was <c>null</c>.</exception>
        public static void SaveAsPng(this Identicon icon, string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                var pngData = icon.GeneratePng();
                stream.Write(pngData, 0, pngData.Length);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> as an Enhanced Metafile (EMF) asynchronously.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        /// <param name="path">The path to the EMF file to create. If the file already exists it will be overwritten.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> was <c>null</c>.</exception>
        public static async Task SaveAsEmfAsync(this Identicon icon, string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                var emf = icon.ToMetafile();
                await stream.WriteAsync(emf, 0, emf.Length);
            }
        }
Ejemplo n.º 16
0
        private static byte[] GenerateBinarySvg(this Identicon icon, bool fragment)
        {
            var svg          = icon.GenerateSvg(fragment);
            var binaryLength = Encoding.UTF8.GetByteCount(svg);
            var preamble     = Encoding.UTF8.GetPreamble();

            var buffer = new byte[binaryLength + preamble.Length];

            for (var i = 0; i < preamble.Length; i++)
            {
                buffer[i] = preamble[i];
            }

            Encoding.UTF8.GetBytes(svg, 0, svg.Length, buffer, preamble.Length);
            return(buffer);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Renders an <see cref="Identicon"/> to a GDI <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="icon">The identicon to render.</param>
        /// <remarks>
        /// The caller should dispose the returned <see cref="Bitmap"/> once it does not
        /// need it anymore.
        /// </remarks>
        public static Bitmap ToBitmap(this Identicon icon)
        {
            var img = new Bitmap(icon.Size, icon.Size);

            try
            {
                using (var g = Graphics.FromImage(img))
                {
                    icon.Draw(g);
                }

                return(img);
            }
            catch
            {
                img.Dispose();
                throw;
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Draws an <see cref="Identicon"/> in a specified WPF drawing context at position (0, 0).
        /// </summary>
        /// <param name="icon">The identicon to draw.</param>
        /// <param name="drawingContext">Drawing context in which the icon will be rendered.</param>
        public static void Draw(this Identicon icon, DrawingContext drawingContext)
        {
            var renderer = new WpfRenderer(drawingContext);

            icon.Draw(renderer);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Saves an <see cref="Identicon"/> icon as a Portable Network Graphics (PNG) file.
        /// </summary>
        /// <param name="icon">The identicon to save.</param>
        public static Stream SaveAsPng(this Identicon icon)
        {
            var pngData = icon.GeneratePng();

            return(new MemoryStream(pngData, false));
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file asynchronously.
 /// </summary>
 /// <param name="icon">The identicon to save.</param>
 /// <param name="stream">The stream to which the SVG data will be written.</param>
 /// <exception cref="ArgumentNullException"><paramref name="stream"/> was <c>null</c>.</exception>
 public static Task SaveAsSvgAsync(this Identicon icon, Stream stream)
 {
     return(icon.SaveAsSvgAsync(stream, false));
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file asynchronously.
 /// </summary>
 /// <param name="icon">The identicon to save.</param>
 /// <param name="path">The path to the SVG file to create. If the file already exists it will be overwritten.</param>
 /// <exception cref="ArgumentNullException"><paramref name="path"/> was <c>null</c>.</exception>
 public static Task SaveAsSvgAsync(this Identicon icon, string path)
 {
     return(icon.SaveAsSvgAsync(path, false));
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Draws an <see cref="Identicon"/> in a specified GDI drawing context at position (0, 0).
        /// </summary>
        /// <param name="icon">The identicon to draw.</param>
        /// <param name="g">Drawing context in which the icon will be rendered.</param>
        public static void Draw(this Identicon icon, Graphics g)
        {
            var renderer = new GdiRenderer(g);

            icon.Draw(renderer);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Draws an <see cref="Identicon"/> in a specified GDI drawing context.
 /// </summary>
 /// <param name="icon">The identicon to draw.</param>
 /// <param name="g">Drawing context in which the icon will be rendered.</param>
 /// <param name="rect">The bounds of the rendered icon, including padding.</param>
 public static void Draw(this Identicon icon, Graphics g, System.Drawing.Rectangle rect)
 {
     icon.Draw(g, rect.ToJdenticon());
 }
Ejemplo n.º 24
0
        /// <summary>
        /// Draws an <see cref="Identicon"/> in a specified GDI drawing context.
        /// </summary>
        /// <param name="icon">The identicon to draw.</param>
        /// <param name="g">Drawing context in which the icon will be rendered.</param>
        /// <param name="rect">The bounds of the rendered icon, including padding.</param>
        public static void Draw(this Identicon icon, Graphics g, Rendering.Rectangle rect)
        {
            var renderer = new GdiRenderer(g);

            icon.Draw(renderer, rect);
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Renders an <see cref="Identicon"/> as a Scalable Vector Graphics (SVG) data string.
 /// </summary>
 /// <param name="icon">The identicon to render as SVG.</param>
 public static string ToSvg(this Identicon icon)
 {
     return(icon.ToSvg(false));
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Saves an <see cref="Identicon"/> as an Enhanced Metafile (EMF).
 /// </summary>
 /// <param name="icon">The identicon to save.</param>
 public static Stream SaveAsEmf(this Identicon icon)
 {
     return(new MemoryStream(icon.ToMetafile(), false));
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Renders an <see cref="Identicon"/> as a Scalable Vector Graphics (SVG) data string.
 /// </summary>
 /// <param name="icon">The identicon to render as SVG.</param>
 /// <param name="fragment">
 /// If <c>true</c> the generated SVG will not be encapsulated in the root svg element making
 /// it suitable to be embedded in another SVG.
 /// </param>
 public static string ToSvg(this Identicon icon, bool fragment)
 {
     return(icon.GenerateSvg(fragment));
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Saves an <see cref="Identicon"/> icon as a Scalable Vector Graphics (SVG) file.
 /// </summary>
 /// <param name="icon">The identicon to save.</param>
 /// <param name="writer">The <see cref="TextWriter"/> to which the SVG data will be written.</param>
 /// <exception cref="ArgumentNullException"><paramref name="writer"/> was <c>null</c>.</exception>
 public static void SaveAsSvg(this Identicon icon, TextWriter writer)
 {
     icon.SaveAsSvg(writer, false);
 }
Ejemplo n.º 29
0
        /// <summary>
        /// Draws an <see cref="Identicon"/> in a specified WPF drawing context.
        /// </summary>
        /// <param name="icon">The identicon to draw.</param>
        /// <param name="drawingContext">Drawing context in which the icon will be rendered.</param>
        /// <param name="rect">The bounds of the rendered icon, including padding.</param>
        public static void Draw(this Identicon icon, DrawingContext drawingContext, Rendering.Rectangle rect)
        {
            var renderer = new WpfRenderer(drawingContext);

            icon.Draw(renderer, rect);
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Draws an <see cref="Identicon"/> in a specified WPF drawing context.
 /// </summary>
 /// <param name="icon">The identicon to draw.</param>
 /// <param name="drawingContext">Drawing context in which the icon will be rendered.</param>
 /// <param name="rect">The bounds of the rendered icon, including padding.</param>
 public static void Draw(this Identicon icon, DrawingContext drawingContext, System.Windows.Rect rect)
 {
     icon.Draw(drawingContext, rect.ToJdenticon());
 }