User Tools

Site Tools


Sidebar

Multimedia

k23:k23.4:k23.4.1:start

23.4.1 Class SvgImage

The SvgImage class represents an SVG graphic that is loaded from a file into memory. You can create this class and receive a new SVG document:

Dim hSvgImage As SvgImage
hSvgImage = New SvgImage ( [ Width As Float, Height As Float ] )

Width and Height are the dimensions of the SVG document in points. If they are not specified, they must be specified later before you can draw on them. The actual dimension of the SVG document is calculated by assuming a resolution of 72 DPI.

The free open source editor Inkscape has the best support for SVG. At https://inkscape.org/de/entwickeln/das-svg-format/ you will find a simple description of the SVG format:

SVG (abbreviation for “Scalable Vector Graphics”) is a free, open source, standardised file format for vector graphics. It is developed and maintained by the W3C (World Wide Web Consortium), the most important international standardisation body for the Internet. It is based on XML (Extensible Markup Language), also a W3C standard format, which is a generic markup language developed to represent any data in a human- and machine-readable format.

If you use the following (XML) text

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200">

  <rect x="50" y="50" width="200" height="100" style="fill:red"/>

</svg>

into the empty file red.svg using a text editor, you will see the following display - for example with the image viewer XViewer:

SVGImage

Figure 23.4.1.1: Display of the red.svg file

23.4.1.1 Properties

The SvgImage class only has two properties:

PropertyData typeDescription
HeightFloatReturns or sets the height of the SVG document in points. A synonym for the height property is H.
WidthFloatReturns or sets the width of the SVG document in points. A synonym for the Width property is W.

Table 23.4.1.1.1 : Properties of the SvgImage class

23.4.1.2 Methods

The SvgImage class provides you with the following four methods:0

MethodDescription
Clear()Deletes the SVG image (in memory).
Paint ( [ X As Float, Y As Float, Width As Float, Height As Float ] )Draws the SVG image on the current drawing area (device).
Resize ( Width As Float, Height As Float )Changes the size of the SVG image. Width is its new width in points. Height is its new height in points.
Save ( Path As String )Saves the SVG image in the specified file path as an SVG file.

Table 23.4.1.2.1 : Methods of the SvgImage class

The following static method loads an SVG document from the file with the specified file path and returns it as a new SVGImage:

Static Function Load ( Path As String ) As SvgImage

Example 1

  • First, a new SVG image is created (1),
  • then the content is loaded from an SVG file and assigned to the SVG image (2) and
  • then the SVG image is drawn in a DrawingArea (3):

Public Sub DrawingArea1_Draw()

'-- Paint the Gambas logo
    Dim hSvgImage As SvgImage			' (1)
 
    hSvgImage = SvgImage.Load("gambas.svg")	' (2)
    hSvgImage.Paint()				' (3)
 
End

Example 2 - Drawing is performed on an SVG image and the modified image is saved.

  • First load the Gambas logo.
  • Then add a nice red ball.
  • Save the modified image:
  Dim hSvgImage As SvgImage
 
  hSvgImage = SvgImage.Load("~/gambas.svg")
 
  Paint.Begin(hSvgImage)
    Paint.Brush = Paint.RadialGradient(200, 140, 40, 215, 115, [Color.RGB(255, 0, 0, 64),
                  Color.White], [1.0, 0.1])
    Paint.Arc(200, 140, 40)
    Paint.Fill()
  Paint.End()
 
  hSvgImage.Save("~/gambas_with_red_ball.svg")

Note: Qt's SVG support has had some bugs for years. It is not known whether it will be better with Qt5.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k23/k23.4/k23.4.1/start.txt · Last modified: 08.01.2024 by emma

Page Tools