Guardando Códigos QR como SVG en IronQR
IronQR no exporta códigos QR directamente a SVG. Aún puedes producir un archivo vectorial redibujando el bitmap generado como rectángulos SVG con una biblioteca de terceros.
La razón es la pila de imágenes subyacente. IronQR utiliza SixLabors.ImageSharp para el procesamiento de imágenes, que maneja formatos rasterizados como PNG, JPEG y BMP, pero no ofrece salida SVG. SVG simplemente no es un formato objetivo que soporte la dependencia.
Solución
Genera el código QR con IronQR como de costumbre, luego recorre sus píxeles y reconstruye la matriz como formas vectoriales usando Svg.NET. Mantienes la generación de códigos QR de IronQR y obtienes control total sobre el estilo y la estructura del SVG.
1. Genera el Código QR y Conviértelo a un Bitmap
Escribe el código QR, guárdalo en un AnyBitmap, luego conviértelo a un System.Drawing.Bitmap para que los píxeles puedan ser leídos individualmente.
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. Redibuja los Píxeles como Rectángulos SVG
Crea un SvgDocument del tamaño del mapa de bits, luego recorre los píxeles y añade un SvgRectangle de 1x1 donde un píxel se lea como negro. La comprobación del umbral (R, G y B todos por debajo de 128) trata los píxeles casi negros como celdas llenas.
// 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. Escribe el Archivo SVG
Guarda el documento en disco una vez que todas las celdas negras han sido añadidas.
// 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")
Esto te da un código QR escalable y basado en vectores, adecuado para impresión o uso en alta resolución.

