# Render to HTML

## Content

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">BitmapGraphicsRendering</span>**, **<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgGraphicsRendering</span>**, and **<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgRendering</span>** generate raster or vector content that can be embedded in HTML.
The rendering targets accept dimensions expressed in pixels, while the rectangle supplied to <span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">Render</span> uses points.
The following example converts chart dimensions from points to pixels by using **<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">C1XLBook.TwipsToPixels</span>**.

```csharp
using C1.Excel;

float width = 450;
float height = 300;

int widthPx = C1XLBook.TwipsToPixels(20 * width);
int heightPx = C1XLBook.TwipsToPixels(20 * height);
```

### Render to PNG

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">BitmapGraphicsRendering</span>** renders chart or diagram content to a raster image.
The following example renders a chart to PNG and converts the output to a Base64-encoded string for embedding in HTML.

```csharp
using System;
using System.IO;
using C1.Chart;
using C1.Excel;
using C1.Util;

// chart is an existing configured FlexChart or FlexDiagram.

float width = 450;
float height = 300;

int widthPx = C1XLBook.TwipsToPixels(20 * width);
int heightPx = C1XLBook.TwipsToPixels(20 * height);

string png;

using (var rendering =
    new BitmapGraphicsRendering(widthPx, heightPx))
{
    chart.Render(rendering, 0, 0, width, height);

    using (var stream = new MemoryStream())
    {
        rendering.Bitmap.SaveAsPng(stream);
        png = Convert.ToBase64String(stream.ToArray());
    }
}
```

The resulting Base64-encoded PNG can be embedded using an HTML image element.

```html
<img src="data:image/png;base64,[PNG DATA]">
```

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">BitmapGraphicsRendering</span>** relies on the imaging components supplied through <span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">C1.Excel</span>.

### Render to SVG With SvgGraphicsRendering

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgGraphicsRendering</span>** generates SVG content through the DS graphics rendering engine.

```csharp
using System.Text;
using System.Xml;
using C1.Chart;
using C1.Excel;
using C1.Util;

// chart is an existing configured FlexChart or FlexDiagram.
// width, height, widthPx, and heightPx are defined
// as shown in the preceding example.

string svg;

using (var rendering =
    new SvgGraphicsRendering(widthPx, heightPx))
{
    chart.Render(rendering, 0, 0, width, height);

    var builder = new StringBuilder();

    rendering.ToDocument().Save(
        builder,
        new XmlWriterSettings
        {
            Encoding = Encoding.UTF8,
            Indent = true
        });

    svg = builder.ToString();
}

svg = svg.Substring(svg.IndexOf("<svg"));
```

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgGraphicsRendering</span>** renders text as vector outlines. The generated text is not selectable as ordinary text.

### Render to SVG With SvgRendering

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgRendering</span>** is designed for resource-constrained platforms such as Blazor, Android, and iOS. The following example renders chart content to SVG. 

```csharp
using C1.Chart;
using C1.Excel;
using C1.Util;

// chart is an existing configured FlexChart or FlexDiagram.
// width, height, widthPx, and heightPx are defined
// as shown in the preceding example.

string svg;

using (var rendering =
    new SvgRendering(widthPx, heightPx))
{
    chart.Render(rendering, 0, 0, width, height);

    svg = rendering.ToText();
}
```

**<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgRendering</span>** produces vector output with selectable text. Text rendering depends on the fonts available in the viewing environment.

### Embed SVG in HTML

SVG content generated by **<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgGraphicsRendering</span>** or **<span class="code" spellcheck="false" data-prosemirror-content-type="mark" data-prosemirror-mark-name="code">SvgRendering</span>** can be embedded directly in an HTML document.
The following example writes generated SVG markup into an HTML file.

```csharp
using System.Xml;

// svg contains the generated SVG markup.

using (var writer = XmlWriter.Create(
    "Chart.html",
    new XmlWriterSettings { Indent = true }))
{
    writer.WriteStartElement("html");
    writer.WriteStartElement("body");

    writer.WriteRaw(svg);

    writer.WriteEndElement();
    writer.WriteEndElement();
}
```

The generated SVG markup can also be incorporated into dynamically generated HTML content.