3

Can the a PictureBox control be used to display vector-based images?

I have read a post from someone claiming that the PictureBox can be used to display vector-based images instead of bitmap images. So the box can be resized and the picture will retain its quality like a vector image should.

So I decided to try it out, although I am looking quality. Is it a problem with my vector images not being fully vector? If this is not possible, is there another way to do this without resorting to WPF?

I'm using VB.NET.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1632018
  • 2,485
  • 10
  • 52
  • 87

3 Answers3

7

Yes, that's possible. It has be a MetaFile, not a Bitmap. Best displayed by using the Paint() event so it can rescale on the fly as the picture box changes size. Sample code:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        PictureBox1.Dock = DockStyle.Fill
        image = New System.Drawing.Imaging.Metafile(New System.IO.MemoryStream(My.Resources.schoolbus))
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawImage(image, New Rectangle(Point.Empty, PictureBox1.ClientSize))
    End Sub

    Private Sub PictureBox1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
        PictureBox1.Invalidate()
    End Sub

    Private image As System.Drawing.Imaging.Metafile

End Class

Where "schoolbus" was a sample .wmf file I added as a resource. The file format is the usual sticking point, there are not that many paint programs still supporting it. You can get one from here.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks again Hans. Im happy to hear I don't need to resort to any third party controls not in the framework. I appreciate that. – user1632018 Sep 26 '12 at 16:16
3

This is an outdated answer from 2012, Adobe SVG view is no longer supported.

PictureBox does not support vector-based images. If you do not want to use WPF, you might want to download an SVG view control. Here's one from Adobe: http://www.adobe.com/svg/viewer/install/

Once you installed it, the control DLL file should be located at, for example, C:\Program Files\Common Files\Adobe\SVG Viewer 3.0\NPSVG3.dll.

If you add the control on your form, you can load the file as stated below.

AxSVGCtl1.SRC = Filename

Using Adobe's SVG control from .NET shows how to use the SVG control in .NET although it is C# code.

WSBT
  • 33,033
  • 18
  • 128
  • 133
0

Use this my friend

                                Dim TheMetaImage As System.Drawing.Imaging.Metafile
                                Dim TheStreamContentResult As System.IO.Stream = Nothing
                                Dim MyHttpClient As New HttpClient
                                TheStreamContentResult = Await MyHttpClient.GetStreamAsync(TheTabPicIcon.ImageLocation).ContinueWith(Function(TT)
                                                                                                                                         Return TT.Result
                                                                                                                                     End Function
            )
                                TheMetaImage = New System.Drawing.Imaging.Metafile(TheStreamContentResult)
                                Using MyG As Graphics = Graphics.FromImage(TheTabPicIcon.Image)
                                    MyG.DrawImage(TheMetaImage, New Rectangle(Point.Empty, TheTabPicIcon.ClientSize))
                                End Using

The "TheTabPicIcon" is the picture box, while the SVG file URL stored in TheTabPicIcon.ImageLocation, you can store it anywhere you want.

Best regards