public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute) { var offset = drawQuietZones ? 0 : 4; var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2); var pixelsPerModule = (double)Math.Min(viewBox.Width, viewBox.Height) / (double)drawableModulesCount; var qrSize = drawableModulesCount * pixelsPerModule; var svgSizeAttributes = sizingMode.Equals(SizingMode.WidthHeightAttribute) ? $@"width=""{viewBox.Width}"" height=""{viewBox.Height}""" : $@"viewBox=""0 0 {viewBox.Width} {viewBox.Height}"""; var svgFile = new StringBuilder($@"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" {svgSizeAttributes} xmlns=""http://www.w3.org/2000/svg"">"); svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(qrSize)}"" height=""{CleanSvgVal(qrSize)}"" fill=""{lightColorHex}"" />"); for (int xi = offset; xi < offset + drawableModulesCount; xi++) { for (int yi = offset; yi < offset + drawableModulesCount; yi++) { if (this.QrCodeData.ModuleMatrix[yi][xi]) { var x = (xi - offset) * pixelsPerModule; var y = (yi - offset) * pixelsPerModule; svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModule)}"" height=""{CleanSvgVal(pixelsPerModule)}"" fill=""{darkColorHex}"" />"); } } } svgFile.Append(@"</svg>"); return(svgFile.ToString()); }
public string GetGraphic(Size viewBox, int pixelsPerModuleX, int pixelsPerModuleY, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute) { var offset = drawQuietZones ? 0 : 4; var drawableModulesXCount = m_xCodeData.ModuleMatrix[0].Count - (drawQuietZones ? 0 : offset * 2); var drawableModulesYCount = m_xCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2); // var SizeX = drawableModulesXCount * pixelsPerModuleX; var SizeY = drawableModulesYCount * pixelsPerModuleY; // var svgSizeAttributes = sizingMode.Equals(SizingMode.WidthHeightAttribute) ? $@"width=""{viewBox.Width}"" height=""{viewBox.Height}""" : $@"viewBox=""0 0 {viewBox.Width} {viewBox.Height}"""; var svgFile = new StringBuilder($@"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" {svgSizeAttributes} xmlns=""http://www.w3.org/2000/svg"">"); svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(SizeX)}"" height=""{CleanSvgVal(SizeY)}"" fill=""{lightColorHex}"" />"); // if( m_bHorizontalOptimization ) { for (int yi = offset; yi < offset + drawableModulesYCount; yi++) { for(int xi = offset; xi < offset + drawableModulesXCount; xi++) { int iWidth=0; while( xi+iWidth < offset + drawableModulesXCount && m_xCodeData.ModuleMatrix[yi][xi+iWidth] ) iWidth++; //if( m_xCodeData.ModuleMatrix[yi][xi+iWidth] ) iWidth++; // if( iWidth > 0 ) { var x = (xi - offset) * pixelsPerModuleX; var y = (yi - offset) * pixelsPerModuleY; //svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModuleX)}"" height=""{CleanSvgVal(pixelsPerModuleY)}"" fill=""{darkColorHex}"" />"); if( y==0 ) svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" width=""{CleanSvgVal(pixelsPerModuleX*iWidth)}"" height=""{CleanSvgVal(pixelsPerModuleY)}""/>"); else svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModuleX*iWidth)}"" height=""{CleanSvgVal(pixelsPerModuleY)}""/>"); // xi+=iWidth-1; } } } } else { for(int xi = offset; xi < offset + drawableModulesXCount; xi++) { for (int yi = offset; yi < offset + drawableModulesYCount; yi++) { int iHeight=0; while( yi+iHeight < offset + drawableModulesYCount && m_xCodeData.ModuleMatrix[yi+iHeight][xi] ) iHeight++; // if( iHeight > 0 ) { var x = (xi - offset) * pixelsPerModuleX; var y = (yi - offset) * pixelsPerModuleY; //svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModuleX)}"" height=""{CleanSvgVal(pixelsPerModuleY)}"" fill=""{darkColorHex}"" />"); if( y==0 ) svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" width=""{CleanSvgVal(pixelsPerModuleX)}"" height=""{CleanSvgVal(pixelsPerModuleY*iHeight)}""/>"); else svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModuleX)}"" height=""{CleanSvgVal(pixelsPerModuleY*iHeight)}""/>"); // yi+=iHeight-1; } } } } svgFile.Append(@"</svg>"); return svgFile.ToString(); }