SVG Embedded Content – Using <image> & <foreignObject>

SVG embedded content refers to external resources such as raster images or HTML fragments that are incorporated into an SVG document. Aspose.SVG supports embedding these resources using the <image> and <foreignObject> elements.

Loading and Displaying Bitmaps – <image> Element

The SVG <image> element allows to include and render bitmaps within an SVG object. It can display image formats JPEG, PNG and the SVG pictures too. Attributes of the <image> element indicate that the contents of a file (a bitmap) should be displayed into a given rectangle (viewport) within the current user coordinate system.

Attributes

How to use the SVG <image> element? The following code snippet shows as a .png and .svg images can embed inside the SVG document:

1<svg width="500" height="400" xmlns="http://www.w3.org/2000/svg">
2    <image href="https://docs.aspose.com/svg/images/svg.png" x="20" y="20" height="180" width="180" />
3    <image href="https://docs.aspose.com/svg/files/shapes.svg" x="250" y="10" height="350" width="350" />
4    <text x="40" y="250">Embedded PNG image</text>
5    <text x="300" y="250">Embedded SVG image</text>
6</svg>

The above code snippet is displayed like this: Embedded PNG and SVG images displayed within an SVG

HTML inside SVG – <foreignObject> Element

SVG is designed to be consistent with other XML languages for describing and rendering of embedded content. The <foreignObject> element allows to include in SVG file the elements in a non-SVG namespace. In the context of a browser, it is most likely HTML. The foreign graphical content can be processed with transformations, filters, clipping, masking and compositing.

Attributes

Let’s see an example:

 1<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
 2    <style>
 3        div {
 4            color: grey;
 5            font: 14px serif;
 6            }
 7    </style>
 8    <circle cx="65" cy="60" r="60" fill="red" fill-opacity="0.1" />
 9    <!-- example of  HTML text embedding in SVG -->
10    <foreignObject x="20" y="20" width="200" height="180">
11    <!--In the context of HTML embedded in the SVG document, the XHTML namespace is mandatory-->
12        <div xmlns="https://www.w3.org/1999/xhtml">
13        Convert SVG to PNG. Aspose.SVG for .NET can read and convert SVG files to PNG, PDF, XPS, and major image formats. 
14        </div>
15    </foreignObject>
16</svg>

HTML element embedded in the SVG document

Using <foreignObject> requires declaring the correct XML namespace for the foreign markup, as shown on line 12 of the example.

Common Mistakes and Fixes

ProblemCauseSolution
Image does not appearMissing width/height attributes on <image>Add explicit width and height values to define the viewport
HTML inside <foreignObject> is invisibleIncorrect or missing XHTML namespaceInclude xmlns="https://www.w3.org/1999/xhtml" on the root HTML element
Image is stretchedpreserveAspectRatio not set or set to noneUse preserveAspectRatio="xMidYMid meet" (or another appropriate value) to maintain aspect ratio
SVG fails to load in older browsersUse of href without fallback xlink:hrefProvide both href and xlink:href attributes for better compatibility

Quick Recipes

Conclusion

Embedding raster images and HTML fragments inside SVG expands the creative possibilities of vector graphics. By mastering the <image> and <foreignObject> elements, you can build rich, interactive visualizations that combine the scalability of SVG with the flexibility of external media.

Next Steps