Ejemplo n.º 1
0
        /// <summary>
        /// Reads an image from the system file and converts it to its base 64 representation with its data uri format ready to be embed.
        /// This method specs that the resource is an image type compatible with pdfmake.
        /// JPEG and PNG formats are supported.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="sourceImageFormat"></param>
        /// <returns></returns>
        public static string LoadImageFromPath(string path, SourceImageFormat sourceImageFormat)
        {
            var byteArray         = File.ReadAllBytes(path);
            var base64StringImage = Convert.ToBase64String(byteArray);

            return($"data:{sourceImageFormat.GetMimeType()};base64,{base64StringImage}");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads an Image from an http request and converts it to its base 64 representation with its data uri format ready to be embed.
        /// This method specs that the resource is an image type compatible with pdfmake.
        /// JPEG and PNG formats are supported.
        /// </summary>
        /// <param name="httpClient"></param>
        /// <param name="httpMethod"></param>
        /// <param name="requestUri"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task <string> LoadImageFromUrlAsync(HttpClient httpClient, string httpMethod, string requestUri, SourceImageFormat sourceImageFormat, CancellationToken cancellationToken)
        {
            var request  = new HttpRequestMessage(new HttpMethod(httpMethod), requestUri);
            var response = await httpClient.SendAsync(request);

            var stream = await response.Content.ReadAsStreamAsync();

            var byteArray = await stream.ToByteArrayAsync(cancellationToken);

            var base64StringImage = Convert.ToBase64String(byteArray);

            return($"data:{sourceImageFormat.GetMimeType()};base64,{base64StringImage}");
        }