0

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.

  • Try [this](http://stackoverflow.com/questions/12585430/can-the-picturebox-be-used-to-display-vector-based-images)? – Ally Feb 28 '17 at 10:30
  • @iProgramIt Yeah did that. The code he wrote works for .wmf files not for .svg files. I want something that works for .svg files. Or maybe something that can convert .svg to .wmf. either of these will work for me. – Prakhar Ganesh Feb 28 '17 at 10:40
  • So, why not just navigate the web browser control to the svg file? I know IE can do that, and I know the web browser control uses the IE rendering engine. – Alexander Ryan Baggett Feb 28 '17 at 20:06

1 Answers1

3

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
Alexander Ryan Baggett
  • 2,170
  • 4
  • 28
  • 55
  • Tried it. The logic makes sense to me still I don't know why it is not working. I mean the webbrowser control is actually not showing anything. It's blank white. The same svg I opened in IE and it opened fine. But not working for webbrowser control. Any extension I am missing or something like that?? I added a debugging art to get that it is reading the file just fine. The problem is with the displaying – Prakhar Ganesh Mar 01 '17 at 11:36
  • Debugged further. Turns out the code is stuck in InitializeComponent() infinitely or is taking too long. Either way it's stuck thus it never reaches the next line. When I removed InitializeComponent() part, it reached the end of the code but still didn't display anything. What is the importance of InitializeComponent() and why is my code stuck in it? – Prakhar Ganesh Mar 01 '17 at 12:00
  • Initialize component is used by the winforms designer generated files. http://stackoverflow.com/questions/12297079/very-simple-definition-of-initializecomponent-method – Alexander Ryan Baggett Mar 01 '17 at 16:14
  • Anyway its working fine without the initializecomponent() line so no worries. – Prakhar Ganesh Mar 03 '17 at 08:17
  • One may delegate svg image loading to the same WebBrowser control, though. This way: `webBrowser1.Url = New Uri(@"file:///./svg.svg")` Worth to mention that absolute file paths are supported as well. – Alexey Khoroshikh Apr 09 '21 at 13:40