Speichern von QR-Codes als SVG in IronQR
IronQR exportiert QR-Codes nicht direkt als SVG. Sie können dennoch eine Vektordatei erstellen, indem Sie das erzeugte Bitmap als SVG-Rechtecke mit einer Drittanbieterbibliothek neu zeichnen.
Der Grund liegt im zugrundeliegenden Bildstapel. IronQR verwendet SixLabors.ImageSharp für die Bildverarbeitung, die Rasterformate wie PNG, JPEG und BMP verarbeitet, aber keine SVG-Ausgabe bietet. SVG ist einfach kein Zielformat, das die Abhängigkeit unterstützt.
Lösung
Erzeugen Sie den QR-Code wie gewohnt mit IronQR, dann gehen Sie durch seine Pixel und bauen Sie die Matrix als Vektorformen mit Svg.NET neu auf. Sie behalten die QR-Generierung von IronQR und gewinnen die volle Kontrolle über das Styling und die Struktur des SVG.
1. Erzeugen Sie den QR-Code und konvertieren Sie in ein Bitmap
Schreiben Sie den QR-Code, speichern Sie ihn in einem AnyBitmap, und wandeln Sie ihn dann in ein System.Drawing.Bitmap um, damit die Pixel einzeln gelesen werden können.
var qr = QrWriter.Write("Hello SVG");
var anyBitmap = qr.Save();
// Convert AnyBitmap to System.Drawing.Bitmap
System.Drawing.Bitmap bit = anyBitmap;
var qr = QrWriter.Write("Hello SVG");
var anyBitmap = qr.Save();
// Convert AnyBitmap to System.Drawing.Bitmap
System.Drawing.Bitmap bit = anyBitmap;
Dim qr = QrWriter.Write("Hello SVG")
Dim anyBitmap = qr.Save()
' Convert AnyBitmap to System.Drawing.Bitmap
Dim bit As System.Drawing.Bitmap = anyBitmap
2. Zeichnen Sie die Pixel als SVG-Rechtecke neu
Erstellen Sie eine SvgDocument, die auf die Bitmap abgestimmt ist, durchlaufen Sie dann die Pixel und fügen Sie an jeder Stelle, an der ein Pixel als schwarz gelesen wird, einen 1x1 SvgRectangle hinzu. Die Schwellenwertprüfung (R, G und B alle unter 128) behandelt nahezu schwarze Pixel als gefüllte Zellen.
// Create new SVG document
var svgDoc = new SvgDocument
{
Width = bit.Width,
Height = bit.Height
};
// Loop through bitmap pixels and add black pixels as SVG rectangles
for (int y = 0; y < bit.Height; y++)
{
for (int x = 0; x < bit.Width; x++)
{
System.Drawing.Color pixel = bit.GetPixel(x, y);
// Check if pixel is black (or close enough)
if (pixel.R < 128 && pixel.G < 128 && pixel.B < 128)
{
var rect = new SvgRectangle
{
X = x,
Y = y,
Width = 1,
Height = 1,
Fill = new SvgColourServer(System.Drawing.Color.Black)
};
svgDoc.Children.Add(rect);
}
}
}
// Create new SVG document
var svgDoc = new SvgDocument
{
Width = bit.Width,
Height = bit.Height
};
// Loop through bitmap pixels and add black pixels as SVG rectangles
for (int y = 0; y < bit.Height; y++)
{
for (int x = 0; x < bit.Width; x++)
{
System.Drawing.Color pixel = bit.GetPixel(x, y);
// Check if pixel is black (or close enough)
if (pixel.R < 128 && pixel.G < 128 && pixel.B < 128)
{
var rect = new SvgRectangle
{
X = x,
Y = y,
Width = 1,
Height = 1,
Fill = new SvgColourServer(System.Drawing.Color.Black)
};
svgDoc.Children.Add(rect);
}
}
}
Imports System.Drawing
' Create new SVG document
Dim svgDoc As New SvgDocument With {
.Width = bit.Width,
.Height = bit.Height
}
' Loop through bitmap pixels and add black pixels as SVG rectangles
For y As Integer = 0 To bit.Height - 1
For x As Integer = 0 To bit.Width - 1
Dim pixel As Color = bit.GetPixel(x, y)
' Check if pixel is black (or close enough)
If pixel.R < 128 AndAlso pixel.G < 128 AndAlso pixel.B < 128 Then
Dim rect As New SvgRectangle With {
.X = x,
.Y = y,
.Width = 1,
.Height = 1,
.Fill = New SvgColourServer(Color.Black)
}
svgDoc.Children.Add(rect)
End If
Next
Next
3. Schreiben Sie die SVG-Datei
Speichern Sie das Dokument auf der Festplatte, sobald jede schwarze Zelle hinzugefügt wurde.
// Save the result as SVG
svgDoc.Write("qr-from-ironqr.svg");
// Save the result as SVG
svgDoc.Write("qr-from-ironqr.svg");
' Save the result as SVG
svgDoc.Write("qr-from-ironqr.svg")
Dies gibt Ihnen einen skalierbaren, vektorbasierten QR-Code, der für den Druck oder die hochauflösende Nutzung geeignet ist.

