/// <summary> /// Converts an <see cref="Image"/> to a Base64 <see cref="String"/>. /// </summary> /// <param name="image">The <see cref="Image"/> instance to convert.</param> /// <returns>A Base64 <see cref="String"/> instance of the image.</returns> /// <exception cref="ArgumentNullException">thrown if the <paramref name="image"/> is <c>Null</c>.</exception> public static string ToBase64(this Image image) { if (image.IsNull()) { throw new ArgumentNullException(nameof(image)); } return(image.ToBase64(new System.Drawing.Imaging.ImageFormat(image.RawFormat.Guid))); }
/// <summary> /// Converts an <see cref="Image"/> to a basic SVG format. /// </summary> /// <param name="image">The <see cref="Image"/> instance to convert.</param> /// <returns>A <see cref="String"/> containing formatted SVG image.</returns> /// <exception cref="ArgumentNullException">thrown if the <paramref name="image"/> is <c>Null</c>.</exception> public static string ToSVG(this Image image) { if (image.IsNull()) { throw new ArgumentNullException(nameof(image)); } string base64 = image.ToBase64(); StringBuilder result = new StringBuilder(); result.AppendLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>"); result.AppendLine($@"<svg version=""1.1"" id=""Layer_1"" xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" x=""0px"" y=""0px"" viewBox=""0 0 {image.Width} {image.Height}"" style=""enable-background:new 0 0 {image.Width} {image.Height};"" xml:space=""preserve"">"); result.AppendLine($@"<image style=""overflow:visible;"" width=""{image.Width}"" height=""{image.Height}"" xlink:href=""data:image/png;base64,{base64}"" ></image>"); result.AppendLine("</svg>"); return(result.ToString()); }
/// <summary> /// Converts an <see cref="Image"/> to a Base64 <see cref="String"/>. /// </summary> /// <param name="image">The <see cref="Image"/> instance to convert.</param> /// <param name="format">A value of <see cref="System.Drawing.Imaging.ImageFormat"/> indicating the format of the <paramref name="image"/>.</param> /// <returns>A Base64 <see cref="String"/> instance of the image.</returns> /// <exception cref="ArgumentNullException">thrown if the <paramref name="image"/> is <c>Null</c>.</exception> public static string ToBase64(this Image image, System.Drawing.Imaging.ImageFormat format) { if (image.IsNull()) { throw new ArgumentNullException(nameof(image)); } using (MemoryStream stream = new MemoryStream()) { // Convert Image to byte[] image.Save(stream, format); byte[] imageBytes = stream.ToArray(); // Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes); return(base64String); } }