@@ -40,30 +40,24 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d
40
40
}
41
41
42
42
public string GetGraphic ( Size viewBox , string darkColorHex , string lightColorHex , bool drawQuietZones = true )
43
- {
44
- var svgFile = new StringBuilder ( @"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" width=""" + viewBox . Width + @""" height=""" + viewBox . Height + @""" xmlns=""http://www.w3.org/2000/svg"">" ) ;
45
- var drawableModulesCount = this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ;
46
- var unitsPerModule = Math . Round ( Convert . ToDouble ( Math . Min ( viewBox . Width , viewBox . Height ) ) / drawableModulesCount , 4 ) ;
47
- if ( unitsPerModule * drawableModulesCount > viewBox . Width )
48
- unitsPerModule -= 0.0001 ;
49
- var offset = drawQuietZones ? 4 * unitsPerModule : 0 ;
50
- var offsetModules = drawQuietZones ? 0 : 4 ;
51
- var qrSize = unitsPerModule * drawableModulesCount ;
52
-
53
- svgFile . AppendLine ( $@ "<rect x=""0"" y=""0"" width=""{ CleanSvgVal ( qrSize ) } "" height=""{ CleanSvgVal ( qrSize ) } "" fill=""" + lightColorHex + @""" />" ) ;
54
- int xi = 0 , yi = 0 ;
55
- for ( var x = 0d ; x < qrSize ; x = x + unitsPerModule )
43
+ {
44
+ var offset = drawQuietZones ? 0 : 4 ;
45
+ var drawableModulesCount = this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : offset * 2 ) ;
46
+ var pixelsPerModule = ( double ) Math . Min ( viewBox . Width , viewBox . Height ) / ( double ) drawableModulesCount ;
47
+ var qrSize = drawableModulesCount * pixelsPerModule ;
48
+ var svgFile = new StringBuilder ( $@ "<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" width=""{ viewBox . Width } "" height=""{ viewBox . Height } "" xmlns=""http://www.w3.org/2000/svg"">") ;
49
+ svgFile . AppendLine ( $@ "<rect x=""0"" y=""0"" width=""{ CleanSvgVal ( qrSize ) } "" height=""{ CleanSvgVal ( qrSize ) } "" fill=""{ lightColorHex } "" />") ;
50
+ for ( int xi = offset ; xi < offset + drawableModulesCount ; xi ++ )
56
51
{
57
- yi = 0 ;
58
- for ( var y = 0d ; y < qrSize ; y = y + unitsPerModule )
52
+ for ( int yi = offset ; yi < offset + drawableModulesCount ; yi ++ )
59
53
{
60
- if ( this . QrCodeData . ModuleMatrix [ yi + offsetModules ] [ xi + offsetModules ] )
54
+ if ( this . QrCodeData . ModuleMatrix [ xi ] [ yi ] )
61
55
{
62
- svgFile . AppendLine ( $@ "<rect x=""{ CleanSvgVal ( x ) } "" y=""{ CleanSvgVal ( y ) } "" width=""{ CleanSvgVal ( unitsPerModule ) } "" height=""{ CleanSvgVal ( unitsPerModule ) } "" fill=""{ darkColorHex } "" />") ;
56
+ var x = ( xi - offset ) * pixelsPerModule ;
57
+ var y = ( yi - offset ) * pixelsPerModule ;
58
+ svgFile . AppendLine ( $@ "<rect x=""{ CleanSvgVal ( x ) } "" y=""{ CleanSvgVal ( y ) } "" width=""{ CleanSvgVal ( pixelsPerModule ) } "" height=""{ CleanSvgVal ( pixelsPerModule ) } "" fill=""{ darkColorHex } "" />") ;
63
59
}
64
- yi ++ ;
65
60
}
66
- xi ++ ;
67
61
}
68
62
svgFile . Append ( @"</svg>" ) ;
69
63
return svgFile . ToString ( ) ;
0 commit comments