private void OnSavePNG(object sender, EventArgs e) { // save file dialog box SaveFileDialog Dialog = new SaveFileDialog(); Dialog.DefaultExt = ".png"; Dialog.AddExtension = true; Dialog.Filter = "Png image files (*.png)|*.png"; Dialog.Title = "Save barcode in PNG format"; Dialog.InitialDirectory = Directory.GetCurrentDirectory(); Dialog.RestoreDirectory = true; Dialog.FileName = "QRCodePNGImage.png"; if (Dialog.ShowDialog() != DialogResult.OK) { return; } // save image as png file QRCodeEncoder.SaveQRCodeToPngFile(Dialog.FileName); // start image editor Process.Start(Dialog.FileName); return; }
/// <summary> /// Command line encode /// </summary> /// <param name="Args">Arguments array</param> public static void Encode ( string[] Args ) { // help if (Args == null || Args.Length < 2) { throw new ArgumentException("help"); } bool TextFile = false; string InputFileName = null; string OutputFileName = null; string Code; string Value; QREncoder Encoder = new QREncoder(); for (int ArgPtr = 0; ArgPtr < Args.Length; ArgPtr++) { string Arg = Args[ArgPtr]; // file name if (Arg[0] != '/' && Arg[0] != '-') { if (InputFileName == null) { InputFileName = Arg; continue; } if (OutputFileName == null) { OutputFileName = Arg; continue; } throw new ArgumentException(string.Format("Invalid option. Argument={0}", ArgPtr + 1)); } // search for colon int Ptr = Arg.IndexOf(':'); if (Ptr < 0) { Ptr = Arg.IndexOf('='); } if (Ptr > 0) { Code = Arg.Substring(1, Ptr - 1); Value = Arg.Substring(Ptr + 1); } else { Code = Arg.Substring(1); Value = string.Empty; } Code = Code.ToLower(); Value = Value.ToLower(); switch (Code) { case "error": case "e": ErrorCorrection EC; switch (Value) { case "low": case "l": EC = ErrorCorrection.L; break; case "medium": case "m": EC = ErrorCorrection.M; break; case "quarter": case "q": EC = ErrorCorrection.Q; break; case "high": case "h": EC = ErrorCorrection.H; break; default: throw new ArgumentException("Error correction option in error"); } Encoder.ErrorCorrection = EC; break; case "module": case "m": if (!int.TryParse(Value, out int ModuleSize)) { ModuleSize = -1; } Encoder.ModuleSize = ModuleSize; break; case "quiet": case "q": if (!int.TryParse(Value, out int QuietZone)) { QuietZone = -1; } Encoder.QuietZone = QuietZone; break; case "text": case "t": TextFile = true; break; default: throw new ApplicationException(string.Format("Invalid argument no {0}, code {1}", ArgPtr + 1, Code)); } } if (TextFile) { string InputText = File.ReadAllText(InputFileName); Encoder.Encode(InputText); } else { byte[] InputBytes = File.ReadAllBytes(InputFileName); Encoder.Encode(InputBytes); } Encoder.SaveQRCodeToPngFile(OutputFileName); return; }