How to Change SVG Background Color in C#

This short tutorial explains how to change SVG background color in C#. It shares the details to set the environment, a list of programming tasks, and a runnable sample code to change SVG fill color in C#. This code can be executed in any of the platforms supporting .NET and does not require any other tool to change the background.

Steps to Change Color of SVG in C#

  1. Set the environment to add Aspose.SVG for .NET to change SVG color
  2. Load the source SVG file into the SVGDocument having a background color
  3. Access the root element in the loaded SVG file
  4. Access the rectangle element from the root element
  5. Set the attribute fill with the desired color
  6. Save the resultant SVG file with modified background color

These steps describe the process to change SVG color in C#. The process is initiated by loading the source SVG file into the SVGDocument class followed by accessing the root element and accessing the rectangle element by queuing the “rect” element. In the final steps, use the SetAttribute() method to set the background color before saving the resultant SVG file.

Code to Set SVG Color in C#

This code snippet demonstrates the process to change color of SVG background image in C#. For changing the background color, we accessed the rect element and used the SetAttribute() method by providing the attribute name and the value. If you want to change the fill color of other elements, use the QuerySelector() method by providing the element name like “circle” and setting the attribute similar to the one set in this sample code.

This article has taught us the process to change color of SVG image in C#. If you want to learn the process to create an SVG file from scratch, refer to the article on how to create SVG in C#.

 English