Esempio n. 1
0
        private static void GenerateQRCode(string payloadString, QRCodeGenerator.ECCLevel eccLevel, string outputFileName, SupportedImageFormat imgFormat, int pixelsPerModule, string foreground, string background)
        {
            using (var generator = new QRCodeGenerator())
            {
                using (var data = generator.CreateQrCode(payloadString, eccLevel))
                {
                    switch (imgFormat)
                    {
                    case SupportedImageFormat.Png:
                    case SupportedImageFormat.Jpg:
                    case SupportedImageFormat.Gif:
                    case SupportedImageFormat.Bmp:
                    case SupportedImageFormat.Tiff:
                        using (var code = new QRCode(data))
                        {
                            using (var bitmap = code.GetGraphic(pixelsPerModule, foreground, background, true))
                            {
                                var actualFormat = new OptionSetter().GetImageFormat(imgFormat.ToString());
                                bitmap.Save(outputFileName, actualFormat);
                            }
                        }
                        break;

                    case SupportedImageFormat.Svg:
                        using (var code = new SvgQRCode(data))
                        {
                            var test = code.GetGraphic(pixelsPerModule, foreground, background, true);
                            using (var f = File.CreateText(outputFileName))
                            {
                                f.Write(test);
                                f.Flush();
                            }
                        }
                        break;

                    case SupportedImageFormat.Xaml:
                        using (var code = new XamlQRCode(data))
                        {
                            var test = XamlWriter.Save(code.GetGraphic(pixelsPerModule, foreground, background, true));
                            using (var f = File.CreateText(outputFileName))
                            {
                                f.Write(test);
                                f.Flush();
                            }
                        }
                        break;

                    case SupportedImageFormat.Ps:
                    case SupportedImageFormat.Eps:
                        using (var code = new PostscriptQRCode(data))
                        {
                            var test = code.GetGraphic(pixelsPerModule, foreground, background, true,
                                                       imgFormat == SupportedImageFormat.Eps);
                            using (var f = File.CreateText(outputFileName))
                            {
                                f.Write(test);
                                f.Flush();
                            }
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(imgFormat), imgFormat, null);
                    }
                }
            }
        }