/// <summary>
        /// Parses src html text and returns found images in base64 representation.
        /// </summary>
        /// <param name="src">Source html.</param>
        /// <param name="imageParsers">Collection of parsers for different representation of the image sources.</param>
        /// <returns>Collection of the parsed images in base64 format with its source as a key.</returns>
        public static Dictionary <string, Base64Image> ExtractInlineImages(string src,
                                                                           IEnumerable <IBase64ImageParser> imageParsers)
        {
            Dictionary <string, Base64Image> result = new Dictionary <string, Base64Image>();
            int cidCounter = 0;

            foreach (Match imgMatch in GetHtmlImgMatches(src))
            {
                Base64Image base64Image = null;
                string      imgSrc      = ExtractImageSource(imgMatch.Value);
                foreach (IBase64ImageParser parser in imageParsers)
                {
                    if (parser.TryParse(imgSrc, out base64Image))
                    {
                        break;
                    }
                }
                if (base64Image != null)
                {
                    if (!result.ContainsKey(imgSrc))
                    {
                        cidCounter++;
                        base64Image.Name = string.Format(ImgNamePattern, cidCounter);
                        result[imgSrc]   = base64Image;
                    }
                }
            }
            return(result);
        }
示例#2
0
        /// <summary>
        /// Converts the string representation of an image in a
        /// <see cref="Terrasoft.Configuration.Mailing.Base64Image"/>.
        /// </summary>
        /// <param name="src">Source of an image to convert.</param>
        /// <param name="image">Instance of the <see cref="Terrasoft.Configuration.Mailing.Base64Image"/> if
        /// parsing was successful, otherwise null.</param>
        /// <returns>Returns true if parsing was successful, otherwise false.</returns>
        public bool TryParse(string src, out Base64Image image)
        {
            image = null;
            bool  isParsed = false;
            var   regExp   = GetRegex();
            Match match    = regExp.Match(src);

            if (match.Success)
            {
                try {
                    image    = GetImage(match);
                    isParsed = true;
                } catch (Exception) {
                }
            }
            return(isParsed);
        }
        /// <summary>
        /// Converts the string representation of an image in a
        /// <see cref="Terrasoft.Configuration.Mailing.Base64Image"/>.
        /// </summary>
        /// <param name="src">Source of an image to convert.</param>
        /// <param name="image">Instance of the <see cref="Terrasoft.Configuration.Mailing.Base64Image"/> if
        /// parsing was successful, otherwise null.</param>
        /// <returns>Returns true if parsing was successful, otherwise false.</returns>
        public bool TryParse(string src, out Base64Image image)
        {
            image = null;
            bool isParsed = false;

            if (src.StartsWith(Base64Src, StringComparison.OrdinalIgnoreCase))
            {
                var    regExp   = new Regex(MimeTypeSearchPattern, RegexOptions.Singleline);
                string mimeType = regExp.Match(src).Value;
                isParsed = !string.IsNullOrEmpty(mimeType);
                if (isParsed)
                {
                    string base64Type = string.Format(Base64MimeTypePattern, mimeType);
                    string content    = src.Replace(base64Type, string.Empty);
                    image = new Base64Image(mimeType, content);
                }
            }
            return(isParsed);
        }