question

blinkor12-6317 avatar image
0 Votes"
blinkor12-6317 asked blinkor12-6317 commented

VB.NET WMF SVG Not Rendering Correctly

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.

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered blinkor12-6317 commented

[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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Castorix31 Last Question (so I won't have to post any more), would there be a way to zoom in/out of this SVG and resize all the graphics proportionally? I know how to resize the graphics by multiplying it by the zoom scale factor but I'm not so sure about the drawn SVG inside the WebBrowser.

0 Votes 0 ·
Castorix31 avatar image
1 Vote"
Castorix31 answered blinkor12-6317 commented

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







webbrowser-svg.jpg (63.9 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can’t display this in a web browser because I will be using graphics to draw objects on it, which is why I need to load it in a picturebox @Castorix31

0 Votes 0 ·

You can hide the WB and copy the bitmap to the Picture Box
Then you can draw over in Paint event
A test with a red circle on your . SVG =>

86537-webbrowser-picturebox-svg.jpg


1 Vote 1 ·

@Castorix31 , What specific function did you use to copy it over?

0 Votes 0 ·
Viorel-1 avatar image
1 Vote"
Viorel-1 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Viorel-1 Added the size tags, the WMF finally has converted correctly. Thanks.

0 Votes 0 ·