79404413

Date: 2025-02-01 04:17:45
Score: 1
Natty:
Report link

I ended up utilizing a variety of things. However, simply reading the bytes did not work, most likely due to the way this SVG is made. Many of the SVG's i was pulling contained css styles that colored the SVG and those were lost when simply reading bytes. Below was my solution.

// Send the request and get the response
var response = await _httpClient.GetAsync(url);

// Ensure the response is successful
response.EnsureSuccessStatusCode();

// Read the response content as a byte array
var svgDocument = SvgDocument.FromSvg<SvgDocument>(await response.Content.ReadAsStringAsync());

//Set the height and width of what I am reading
svgDocument.Width = 1024;
svgDocument.Height = 1024;

// Set up a Bitmap to render the SVG
using (var bitmap = new Bitmap(1024, 1024))
{
    using (var graphics = Graphics.FromImage(bitmap))
    {
        // Clear the canvas with a white background (optionally use transparent)
        graphics.Clear(Color.White);

        // Render the SVG content to the bitmap
        svgDocument.Draw(graphics);

        // Convert the Bitmap to a byte array
        using (var memoryStream = new MemoryStream())
        {
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            return memoryStream.ToArray(); //This is the bytes that was needed
        }
    }
}
Reasons:
  • Blacklisted phrase (1): did not work
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: VoidSharp