<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sitecore Xperiences &#187; Front-end</title>
	<atom:link href="https://blog.peplau.com.br/category/front-end/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.peplau.com.br</link>
	<description>The things I&#039;ve seen as a Sitecore Professional</description>
	<lastBuildDate>Sun, 09 Mar 2025 21:54:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>Advanced display of SVG Images in Sitecore</title>
		<link>https://blog.peplau.com.br/advanced-display-of-svg-images-in-sitecore/</link>
		<comments>https://blog.peplau.com.br/advanced-display-of-svg-images-in-sitecore/#comments</comments>
		<pubDate>Fri, 31 Jul 2015 23:56:39 +0000</pubDate>
		<dc:creator><![CDATA[Rodrigo Peplau]]></dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Experience Editor]]></category>
		<category><![CDATA[Front-end]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.peplau.com.br/?p=219</guid>
		<description><![CDATA[<div class="lr_horizontal_share" data-share-url="https://blog.peplau.com.br/advanced-display-of-svg-images-in-sitecore/"></div>SVG Images are basically vector images (coordinates, objects and properties) with data that can be manipulated with CSS styling &#8211; check out this article for details on how that works. In short, to take advantage of CSS styling over SVG files, you must throw the XML content of the SVG file, directly into the HTML markup. So instead of the traditional [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>SVG Images are basically vector images (coordinates, objects and properties) with data that can be manipulated with CSS styling &#8211; check out <a href="https://css-tricks.com/using-svg/" target="_blank">this article</a> for details on how that works.</p>
<p>In short, to take advantage of CSS styling over SVG files, you must throw the XML content of the SVG file, directly into the HTML markup. So instead of the traditional &lt;img tag:</p>
<pre>&lt;img src="image.svg"</pre>
<p>You must throw inline the content of the SVG file, which will be something like:</p>
<pre>&lt;svg ...&gt; &lt;ellipse class="chair" .../&gt; <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>svg</span><span class="token punctuation">&gt;</span></span></pre>
<p>In Sitecore that means you can&#8217;t use the standard <strong>sc:Image</strong> control to render the content of an Image Field &#8211; that will render your SVG as an &lt;img HTML tag, which can&#8217;t be touched by CSS styling&#8230;</p>
<p><strong>Usercontrol</strong></p>
<p>In order to have something that is compatible both with CSS Styling and Sitecore&#8217;s Page Editor mode, my proposal is to have a custom asp usercontrol like this:</p>
<p><em><strong>Image.ascx</strong></em></p>
<pre>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Image.ascx.cs" Inherits="Layouts.UserControls.Image" %&gt;
&lt;sc:Image runat="server" ID="scImage" /&gt;
&lt;asp:Literal runat="server" ID="litImage"&gt;&lt;/asp:Literal&gt;</pre>
<p><em><strong> Image.ascx.cs (Codebehind)</strong></em></p>
<pre>namespace Layouts.UserControls
{
 public partial class Image : UserControl
 {
   /// &lt;summary&gt;
   /// Component's Field
   /// &lt;/summary&gt;
   public string Field { get; set; }</pre>
<pre>   /// &lt;summary&gt;
   /// Component's Item (Default=Context)
   /// &lt;/summary&gt;
   public Item Item
   {
      get { return _item ?? Sitecore.Context.Item; }
      set { _item = value; }
   }
   private Item _item;</pre>
<pre>   protected void Page_Load(object sender, EventArgs e)
   {
     // When editing in Page Editor mode, always use an &lt;sc:image
     // If no field is set, escape and let it break
     if (Sitecore.Context.PageMode.IsPageEditorEditing || string.IsNullOrEmpty(Field))
     {
        ShowScImage();
        return;
     }</pre>
<pre>     // Escape if field is not found
     var referencedImageField = (Sitecore.Data.Fields.ImageField)Item.Fields[Field];
     if (referencedImageField == null)
     {
       ShowScImage();
       return;
     }</pre>
<pre>     // Get referenced image - Scape if image is not found
     var imageItem = referencedImageField.MediaItem;
     if (imageItem == null)
     {
        ShowScImage();
        return;
     }</pre>
<pre>     // If this is not an svg use a normal &lt;sc:Image
     var imageMediaItem = (MediaItem) imageItem;
     if (!imageMediaItem.Extension.ToLower().Contains("svg"))
     {
        ShowScImage();
        return;
     }</pre>
<pre>     ShowSvgImage(imageMediaItem);
 }</pre>
<pre> public static string GetMediaUrlWithServer(MediaItem mediaItem, Item item = null)
 {
    item = item ?? Sitecore.Context.Item;
    var options = new UrlOptions { AlwaysIncludeServerUrl = true, AddAspxExtension = false };
    var itemUrl = LinkManager.GetItemUrl(item, options);
    var mediaOptions = new MediaUrlOptions { AbsolutePath = true };
    var mediaUrl = MediaManager.GetMediaUrl(mediaItem, mediaOptions);
    return itemUrl + mediaUrl;
 }</pre>
<pre> private void ShowSvgImage(MediaItem mediaItem)
 {
    litImage.Visible = true;
    scImage.Visible = false;</pre>
<pre>    // Get string from Image Stream
    var stream = mediaItem.GetMediaStream();
    var reader = new StreamReader(stream);
    var svgString = reader.ReadToEnd();
    reader.Close();
    stream.Close();</pre>
<pre>    // Get SVG tag from string</pre>
<pre>    // No SVG tag found - fallback to &lt;sc:Image
    if (!svgString.Contains("&lt;svg"))
    {
       ShowScImage();
       return;
    }</pre>
<pre>    svgString = svgString.Substring(svgString.IndexOf("&lt;svg",StringComparison.Ordinal));
    litImage.Text = svgString;
 }</pre>
<pre> private void ShowScImage()
 {
    litImage.Visible = false;
    scImage.Visible = true;</pre>
<pre>    scImage.Item = Item;
    scImage.Field = Field;
    ReflectAttributesToControl(scImage); 
 }</pre>
<pre> private void ReflectAttributesToControl(WebControl control)
 {
    foreach (string key in Attributes.Keys)
     control.Attributes[key] = Attributes[key];
 }
}</pre>
<p><strong>Streaming</strong></p>
<p>The proposed solution makes use of Streaming for better performance, so rendering times are not affected.</p>
<p><strong>How to Use</strong></p>
<p>Now we have our custom image rendering control that renders inline SVG images when you are in Preview or Normal mode, and is compatible with Sitecore&#8217;s Page Editor mode.</p>
<p>To use, first you have to register the Usercontrol:</p>
<pre>&lt;%@ Register TagPrefix="custom" TagName="Image" Src="~/layouts/UserControls/Image.ascx" %&gt;</pre>
<p>Then use it just like you&#8217;d do with a standard sc:Image control:</p>
<pre>&lt;custom:Image runat="server" ID="imgSvgImage" Field="My Field Name" /&gt;</pre>
<p>At the code behind you can also access the properties &#8220;Field&#8221; and &#8220;Item&#8221; just like the normal Image control:</p>
<pre>imgClear.Item = Sitecore.Context.Item;</pre>
<p>And that&#8217;s all!</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.peplau.com.br/advanced-display-of-svg-images-in-sitecore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
