在IronQR中将二维码保存为SVG
This article was translated from English: Does it need improvement?
Translated
View the article in English
IronQR不直接导出二维码为SVG。 您仍然可以通过用第三方库重新绘制生成的位图为SVG矩形来生成矢量文件。
原因在于底层的图像栈。 IronQR使用SixLabors.ImageSharp进行图像处理,处理的光栅格式包括PNG、JPEG和BMP,但不提供SVG输出。 SVG并不是依赖项支持的目标格式。
解决方案
照常使用IronQR生成二维码,然后遍历其像素并使用Svg.NET将矩阵重建为矢量形状。 您保持IronQR的二维码生成,并完全控制SVG的样式和结构。
1. 生成二维码并转换为位图
编写QR码,将其保存到System.Drawing.Bitmap,以便可以单独读取像素。
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. 重新绘制像素为SVG矩形
创建一个与位图大小相同的SvgRectangle。 阈值检查(128)将近黑色像素视为填充单元。
// 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. 写入SVG文件
一旦添加了每个黑色单元格,将文档保存到磁盘。
// 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
这样可以生成一个可缩放的、基于矢量的二维码,适合打印或高分辨率使用。
准备开始了吗?
Nuget 下载 70,398 | 版本: 2026.7 刚刚发布

