Enregistrer des codes QR en SVG dans IronQR

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronQR n'exporte pas directement les codes QR en SVG. Vous pouvez toujours produire un fichier vectoriel en redessinant le bitmap généré sous forme de rectangles SVG avec une bibliothèque tierce.

La raison en est la pile d'images sous-jacente. IronQR utilise SixLabors.ImageSharp pour le traitement des images, qui gère les formats raster tels que PNG, JPEG et BMP mais n'offre pas de sortie SVG. SVG n'est tout simplement pas un format cible supporté par la dépendance.

Solution

Générez le code QR avec IronQR comme d'habitude, puis parcourez ses pixels et reconstruisez la matrice sous forme de formes vectorielles utilisant Svg.NET. Vous conservez la génération de QR d'IronQR et gagnez un contrôle total sur le style et la structure de l'SVG.

1. Générez le Code QR et Convertissez-le en Bitmap

Écrivez le code QR, enregistrez-le dans un AnyBitmap, puis convertissez-le en un System.Drawing.Bitmap afin que les pixels puissent être lus individuellement.

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
$vbLabelText   $csharpLabel

2. Redessinez les Pixels sous forme de Rectangles SVG

Créez un SvgDocument adapté au bitmap, puis parcourez les pixels et ajoutez un SvgRectangle de 1x1 partout où un pixel est lu comme noir. La vérification du seuil (R, G, et B tous en dessous de 128) considère les pixels presque noirs comme des cellules remplies.

// 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
$vbLabelText   $csharpLabel

3. Écrivez le Fichier SVG

Enregistrez le document sur le disque une fois que chaque cellule noire a été ajoutée.

// 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")
$vbLabelText   $csharpLabel

Cela vous donne un code QR évolutif basé sur des vecteurs, adapté pour l'impression ou une utilisation à haute résolution.

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 70,398 | Version : 2026.7 vient de sortir
Still Scrolling Icon

Vous faites encore défiler ?

Vous voulez une preuve rapidement ? PM > Install-Package IronQR
exécuter un échantillon regarder votre URL devenir un code QR.