VB.NET WMF SVG Not Rendering Correctly

blinkor12 101 Reputation points
2021-04-11T01:54:28.097+00:00

I'm trying to render a SVG inside a PictureBox with VB.NET. I know that I have to convert my SVG file to a WMF file type before displaying the image inside a PictureBox. I'm using this code to render the graphic inside my PictureBox.

Private image As System.Drawing.Imaging.Metafile
image = New System.Drawing.Imaging.Metafile("C:\users\admin\Documents\convertedSVGfile.wmf")

   Private Sub outputMapView_Paint(sender As Object, e As PaintEventArgs) Handles outputMapView.Paint
        e.Graphics.DrawImage(image, New Rectangle(Point.Empty, outputMapView.ClientSize))

    End Sub

However, when I run the code, my PictureBox comes out blank with no image visible. I know that this may be some compatibility issue with the SVG during the rendering/conversion process, but I don't know what is specifically wrong with it. The SVG renders fine in any web browser (such as Chrome or Edge).

I've attached my default SVG file and the .WMF file (converted from the SVG) I used in my program before the conversion.

Can anyone help me with this?

Default SVG File:Google Drive Link
Converted WMF File: Google Drive Link

Thanks.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,481 Reputation points
    2021-04-12T06:49:17.247+00:00

    [Cannot post in comment... 1000 characters, ...]

    Test .SVG copied to PictureBox from WebBrowser with IViewObject interface =>

    Imports System.Runtime.InteropServices  
      
    Public Class Form1  
        <ComImport()>  
        <Guid("0000010d-0000-0000-C000-000000000046")>  
        <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>  
        Public Interface IViewObject  
            <PreserveSig>  
            Function Draw(dwDrawAspect As UInteger, lindex As Integer, pvAspect As IntPtr, ptd As IntPtr, hdcTargetDev As IntPtr, hdcDraw As IntPtr, lprcBounds As Rectangle,  
                          lprcWBounds As IntPtr, pfnContinue As IntPtr, dwContinue As Integer) As Integer  
            Function GetColorSet(dwDrawAspect As UInteger, lindex As Integer, pvAspect As IntPtr, ptd As IntPtr, hicTargetDev As IntPtr, <Out> ppColorSet As IntPtr) As Integer  
            Function Freeze(dwDrawAspect As UInteger, lindex As Integer, pvAspect As IntPtr, <Out> pdwFreeze As UInteger) As Integer  
            Function Unfreeze(dwFreeze As UInteger) As Integer  
            Function SetAdvise(aspects As UInteger, advf As UInteger, pAdvSink As IntPtr) As Integer  
            Function GetAdvise(<Out> pAspects As UInteger, <Out> pAdvf As UInteger, <Out> ppAdvSink As IntPtr) As Integer  
        End Interface  
      
        Public Enum DVASPECT  
            DVASPECT_CONTENT = 1  
            DVASPECT_THUMBNAIL = 2  
            DVASPECT_ICON = 4  
            DVASPECT_DOCPRINT = 8  
        End Enum  
      
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint  
            e.Graphics.DrawEllipse(New Pen(Color.Red, 2.0F), 50, 50, PictureBox1.Size.Width - 100, PictureBox1.Size.Height - 100)  
        End Sub  
      
        Friend WithEvents WebBrowser1 As WebBrowser  
        Friend WithEvents PictureBox1 As PictureBox  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            WebBrowser1 = New System.Windows.Forms.WebBrowser()  
            PictureBox1 = New System.Windows.Forms.PictureBox()  
            WebBrowser1.Location = New System.Drawing.Point(36, 68)  
            WebBrowser1.MinimumSize = New System.Drawing.Size(20, 20)  
            WebBrowser1.Name = "WebBrowser1"  
            WebBrowser1.Size = New System.Drawing.Size(500, 500)  
      
            WebBrowser1.ScrollBarsEnabled = False  
            WebBrowser1.Url = New Uri(String.Format("E:\Sources\VB_SVG\originalSvg.svg"))  
            WebBrowser1.Visible = False  
      
            PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle  
            PictureBox1.Location = New System.Drawing.Point(20, 20)  
            PictureBox1.Name = "PictureBox1"  
            PictureBox1.Size = New System.Drawing.Size(500, 500)  
      
            Controls.Add(PictureBox1)  
            Controls.Add(WebBrowser1)  
            Name = "Form1"  
      
            ClientSize = New System.Drawing.Size(540, 540)  
            CenterToScreen()  
        End Sub  
      
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted  
            ' Arbitrary size to test  
            Dim nWidth = 500  
            Dim nHeight = 500  
            Dim bmp = New Bitmap(nWidth, nHeight)  
            Dim gr As Graphics = Graphics.FromImage(bmp)  
            Dim hDC = gr.GetHdc()  
            Dim rc = New Rectangle(0, 0, nWidth, nHeight)  
            Dim viewObject = CType(WebBrowser1.Document.DomDocument, IViewObject)  
            viewObject.Draw(DVASPECT.DVASPECT_CONTENT, -1, CType(0, IntPtr), CType(0, IntPtr), CType(0, IntPtr), hDC, rc, CType(0, IntPtr), CType(0, IntPtr), 0)  
            gr.ReleaseHdc(hDC)  
            PictureBox1.Image = bmp  
        End Sub  
    End Class  
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Castorix31 81,481 Reputation points
    2021-04-11T09:06:24.797+00:00

    Your .WMF is empty

    But you don't need to convert the .SVG

    You can display it with a WebBrowser control
    or with Direct2D (ID2D1DeviceContext5.CreateSvgDocument, ID2D1DeviceContext5.DrawSvgDocument), but it needs more code (P/Invoke)

    Test with a WebBrowser control =>

    WebBrowser1.Url = New Uri(String.Format("E:\Sources\VB_SVG\originalSvg.svg"))  
    

    86611-webbrowser-svg.jpg

    1 person found this answer helpful.

  2. Viorel 112.1K Reputation points
    2021-04-11T11:50:51.887+00:00

    Does it work if you add the next sizes to SVG file, the re-export to WMF or another image format?

    <?xml version="1.0" encoding="utf-8"?>
    <svg . . . width="450" height="530" >
        . . .
    </svg>
    

    How did you convert the image?

    1 person found this answer helpful.