Search code examples
vb.netvisual-studiosvgwebbrowser-controlpicturebox

Display a svg file in vb.net


I have gone through a lot of posts with this question. I am trying to develop an app in visual studio in vb.net and i want to let the user open a dialog box and choose a SVG file which can then be displayed inside the app.

I saw a lot of solutions on the internet but these are the few common problems with all the solutions I saw online -

  1. Looks like nobody had any problem in displaying SVGs after 2010 and most of the solutions given till then include installation of Adobe SVG viewer which is now discontinued by Adobe.
  2. Most of the people create an SVG inside their code and display that simple SVG. I don't want that because I have no idea what the SVG is like. What I want is the user to select SVG in real time and display that.

I have tried to make it work in both picturebox and in webbrowser control but I could not find anything significant in any of those.

Also I was hoping if you can please give me the code in visual basic not in c#. However if you do find some relevant code in c#, do link it because I might try to convert the code if it works.

Don't worry about open dialog box and everything, just open an svg where the whole address of svg is written at a single place and I can take it from there.


Solution

  • Its pretty straight forward to load an SVG file in the web browser control. You can do it this way via the DocumentText property.

    Public Class Form1
    
       Public Sub New()
           Dim text As String = System.IO.File.ReadAllText("svg.svg")
           InitializeComponent()
           WebBrowser1.DocumentText = text
       End Sub
    
    End Class