Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert SVG to JPG in C#, load the SVG document or open the SVG file as a stream, create
ImageSaveOptions with ImageFormat.Jpeg, and call
Converter.ConvertSVG() with the source, options, and output path or stream provider.
JPG is a compact raster image format commonly used for previews, thumbnails, and shareable image snapshots. SVG to JPG conversion is useful when vector graphics must be rendered as broadly supported image files and transparency is not required.
Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.
The static
Converter class provides a compact way to render SVG content to JPG. The example below converts SVG to JPG with ImageSaveOptions configured for JPEG output.
1// Convert SVG to JPG using C#
2
3// Invoke the ConvertSVG() method for SVG to JPG conversion
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-with-single-line.jpg"));Use this workflow when the SVG source exists on disk and the JPG should be saved to a file path.
The workflow is:
SVGDocument.ImageSaveOptions instance with ImageFormat.Jpeg. The default Format value is PNG, so the format must be set for JPG output.Converter.ConvertSVG(source, options, savePath). 1// Convert SVG to JPG in C#
2
3// Prepare SVG code
4string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
5 "<circle cx ='100' cy ='100' r ='55' fill='green' stroke='red' stroke-width='10' />" +
6 "</svg>";
7
8// Prepare a path for converted file saving
9string savePath = Path.Combine(OutputDir, "circle.jpg");
10
11// Create an instance of the ImageSaveOptions class
12ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
13
14// Convert SVG to JPG
15Converter.ConvertSVG(code, ".", options, savePath);Use JPG output when you need compact previews, thumbnails, or image snapshots and file size matters more than transparency or lossless edges. JPG is usually not the best choice for SVG icons, diagrams, UI graphics, or transparent artwork. For crisp lossless rendering, use SVG to PNG. If the output must remain a document, use SVG to PDF.
Use ImageSaveOptions when the generated JPG needs custom rendering settings. To customize SVG to JPG conversion:
ImageSaveOptions with ImageFormat.Jpeg.PageSetup, BackgroundColor, resolution, or UseAntialiasing.Converter.ConvertSVG().| Option | Use it to control |
|---|---|
Format | The output image format. The default value is PNG; use ImageFormat.Jpeg for JPG output. |
BackgroundColor | The color used to fill the image background. The default value is transparent. |
PageSetup | Page size, margins, and page layout settings. |
HorizontalResolution and VerticalResolution | Output resolution values used by the rendering process. The default value is 300 dpi for each property. |
UseAntialiasing | Image rendering quality. Antialiasing is enabled by default. |
Text | Text rendering configuration through TextOptions. |
1// Convert SVG to JPG in C# with custom background, resolution, and antialiasing settings
2
3// Prepare a path to a source SVG file
4string documentPath = Path.Combine(DataDir, "flower.svg");
5
6// Prepare a path for converted file saving
7string savePath = Path.Combine(OutputDir, "flower-options.jpg");
8
9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Create an instance of the ImageSaveOptions class. Set up the resolutions, SmoothingMode and change the background color to AliceBlue
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
14{
15 UseAntialiasing = true,
16 HorizontalResolution = 200,
17 VerticalResolution = 200,
18 BackgroundColor = System.Drawing.Color.AliceBlue
19};
20
21// Convert SVG to JPG
22Converter.ConvertSVG(document, options, savePath);Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, and services that decide where rendered image output is stored.
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}The following example converts SVG to JPG with MemoryStreamProvider.
1// Convert SVG to JPG in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Prepare SVG code
7string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
8 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />" +
9 "</svg>";
10
11// Convert SVG to JPG using the MemoryStreamProvider
12Converter.ConvertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
13
14// Get access to the memory stream that contains the result data
15MemoryStream memory = streamProvider.Streams.First();
16memory.Seek(0, SeekOrigin.Begin);
17
18// Flush the result data to the output file
19using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.jpg")))
20{
21 memory.CopyTo(fs);
22}Use the online SVG to JPG Converter for quick manual conversion. Use Aspose.HTML for .NET when SVG to JPG conversion must run inside a C# application, service, or document pipeline.
You can evaluate Aspose.HTML for .NET before purchasing a license, but converted documents created in evaluation mode contain watermarks and are limited to four pages. For unrestricted testing, request a free 30-day Temporary License. For occasional manual conversions, use the free online SVG to JPG Converter.
See Licensing Aspose.HTML for .NET for evaluation limitations and license setup.
JPG does not support transparency. Set ImageSaveOptions.BackgroundColor explicitly so transparent areas are filled with the intended color instead of relying on a default background. Use PNG when the output must retain transparency.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.