将SVG或Base30转换为图像文件

时间:2017-08-18 15:43:59

标签: vb.net

我正在尝试将SVG文件转换为任何格式的图像文件。 我正在从base30值创建SVG文件。

Public Sub SaveSignature()
    Dim B30 As New Base30Converter

    Dim Img = B30.GetData("aQ4895d6d7j5h94840Z39baa6bfb6430Y4538d6a49c59db8863003240Z4374b7ek7j4a33Y255e8j5d6Z52Y3b2428385e9n1vd1Bfq6a4Z4Y3949456Z1e91wi2B2N1u2Eh1H9bY5j81ye1w1u8e5724983329d8pe1Ai1Nf1V1Uft9d5_7WZ266594c4g7gc36452542000Y2648a67334524112100Z1235766Y64Z9ah593420Y75g7d483401000Z34Y133Z332Y7e29262300Z43a3652Y26252556400Z2ig6h2200473720Y2263542006736424241000Z826010")
    Dim svg = SVGConverter.ToSVG(Img)

    Dim sw As New System.IO.StreamWriter("D:\Sign.svg", False)
    sw.Write(svg)
    sw.Close()
    Dim bmp As Bitmap = renderFile("D:\Sign.svg")
    Dim g As Graphics
    g = Graphics.FromImage(bmp)
    Using g
        g.Clear(Color.White)
        g.DrawImageUnscaled(bmp, 0, 0)
    End Using
    bmp.Save("D:\Sign.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
    bmp.Save("D:\Sign.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    bmp.Save("D:\Sign.emf", System.Drawing.Imaging.ImageFormat.Emf)
End Sub

Public Function renderFile(filename As String) As Bitmap
    Dim displaySize As System.Drawing.Size
    displaySize.Width = 300
    displaySize.Height = 200
    Dim svgDoc As SvgDocument = SvgDocument.Open(filename)
    Dim svgSize = svgDoc.GetDimensions()

    If svgSize.Width = 0 Then
        Throw New Exception("SVG does not have size specified. Cannot work with it.")
    End If

    Dim displayProportion = (displaySize.Height * 1.0F) / displaySize.Width
    Dim svgProportion = svgSize.Height / svgSize.Width

    Dim scalingFactor As Single = 0.0F
    Dim padding As Integer = 10

    If displayProportion > svgProportion Then
        scalingFactor = ((displaySize.Width - padding * 2) * 1.0F) / svgSize.Width
    Else
        scalingFactor = ((displaySize.Height - padding * 2) * 1.0F) / svgSize.Height
    End If

    If scalingFactor < 0 Then
        Throw New Exception("Viewing area is too small to render the image")
    End If

    Dim centeringX As Integer = Convert.ToInt16((displaySize.Width - (padding + CInt(svgDoc.Width) * scalingFactor)) / 2)
    Dim centeringY As Integer = Convert.ToInt16((displaySize.Height - (padding + CInt(svgDoc.Height) * scalingFactor)) / 2)

    svgDoc.Transforms = New SvgTransformCollection()
    svgDoc.Transforms.Add(New SvgTranslate(padding + centeringX, padding + centeringY))
    svgDoc.Transforms.Add(New SvgScale(scalingFactor))
    svgDoc.Width = New SvgUnit(svgDoc.Width.Type, displaySize.Width)
    svgDoc.Height = New SvgUnit(svgDoc.Height.Type, displaySize.Height)
    Return svgDoc.Draw()
End Function

在所有这些处理之后,图像文件留下空文件。

我从asp应用程序中的签名面板获取的base30值。

0 个答案:

没有答案