Exemplo n.º 1
0
        /// <inheritdoc cref="Identicon(IHtmlHelper, Jdenticon.Identicon, int, string, ExportImageFormat)" />
        /// <summary>
        /// Renders an identicon as an IMG tag.
        /// </summary>
        /// <param name="helper">The <see cref="IHtmlHelper"/>.</param>
        /// <param name="hash">The hash that will be used as base for the icon. Must contain at least 6 bytes.</param>
        /// <param name="size">The size of the generated icon in pixels.</param>
        /// <param name="alt">The alt attribute of the rendered image.</param>
        /// <param name="style">The icon style.</param>
        /// <param name="format">The file format of the generated icon.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="size"/> was less than 1.</exception>
        /// <exception cref="ArgumentException"><paramref name="hash"/> was too short.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="helper"/> or <paramref name="hash"/> was <c>null</c>.</exception>
        public static IHtmlContent Identicon(this IHtmlHelper helper, byte[] hash, int size, string alt = null,
                                             ExportImageFormat format = ExportImageFormat.Png, IdenticonStyle style = null)
        {
            if (helper == null)
            {
                throw new ArgumentNullException(nameof(helper));
            }
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }
            if (hash.Length < 6)
            {
                throw new ArgumentException("The specified hash is too short. At least 6 bytes are required.", nameof(hash));
            }
            if (size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(size), size, "The size should be 1 pixel or larger");
            }

            var url = IdenticonUrl.Create(helper.ViewContext.HttpContext, hash, size, format, style);

            var img = new TagBuilder("img");

            img.Attributes["src"]    = url;
            img.Attributes["width"]  = size.ToString();
            img.Attributes["height"] = size.ToString();
            img.Attributes["alt"]    = alt ?? "";
            img.TagRenderMode        = TagRenderMode.SelfClosing;

            return(img);
        }
Exemplo n.º 2
0
        /// <inheritdoc cref="Identicon(IUrlHelper, Jdenticon.Identicon, int, ExportImageFormat)" />
        /// <summary>
        /// Generates an URL to an identicon.
        /// </summary>
        /// <param name="helper">The <see cref="IUrlHelper"/>.</param>
        /// <param name="hash">The hash that will be used as base for the icon. Must contain at least 6 bytes.</param>
        /// <param name="size">The size of the generated icon in pixels.</param>
        /// <param name="style">The icon style.</param>
        /// <param name="format">The file format of the generated icon.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="size"/> was less than 1.</exception>
        /// <exception cref="ArgumentException"><paramref name="hash"/> was too short.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="helper"/> or <paramref name="hash"/> was <c>null</c>.</exception>
        public static string Identicon(this IUrlHelper helper, byte[] hash, int size, ExportImageFormat format = ExportImageFormat.Png, IdenticonStyle style = null)
        {
            if (helper == null)
            {
                throw new ArgumentNullException(nameof(helper));
            }
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }
            if (hash.Length < 6)
            {
                throw new ArgumentException("The specified hash is too short. At least 6 bytes are required.", nameof(hash));
            }
            if (size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(size), size, "The size should be 1 pixel or larger");
            }

            return(IdenticonUrl.Create(helper.ActionContext.HttpContext, hash, size, format, style));
        }