public Bitmap CreateBarCode( string inputPath, BarCodeParameters parameters, FfmpegWrapper ffmpeg, CancellationToken cancellationToken, IProgress <double> progress = null, Action <string> log = null) { Bitmap finalBitmap = null; Graphics finalBitmapGraphics = null; Graphics GetDrawingSurface(int width, int height) { if (finalBitmap == null) { finalBitmap = new Bitmap(width, height); finalBitmapGraphics = Graphics.FromImage(finalBitmap); } return(finalBitmapGraphics); } var barCount = (int)Math.Round((double)parameters.Width / parameters.BarWidth); var source = ffmpeg.GetImagesFromMedia(inputPath, barCount, cancellationToken, log); int?finalBitmapHeight = null; int x = 0; foreach (var image in source) { if (finalBitmapHeight == null) { finalBitmapHeight = parameters.Height ?? image.Height; } var surface = GetDrawingSurface(parameters.Width, finalBitmapHeight.Value); surface.DrawImage(image, x, 0, parameters.BarWidth, finalBitmapHeight.Value); x += parameters.BarWidth; progress?.Report((double)x / parameters.Width); image.Dispose(); } finalBitmapGraphics?.Dispose(); return(finalBitmap); }
public CompleteBarCodeGenerationParameters GetValidatedParameters( string rawInputPath, string rawOutputPath, string rawBarWidth, string rawImageWidth, string rawImageHeight, bool useInputHeightForOutput, bool generateSmoothVersion, Func <string, bool> shouldOverwriteOutput) { var inputPath = rawInputPath.Trim(new[] { '"' }); var outputPath = rawOutputPath?.Trim(new[] { '"' }); void ValidateOutputPath(ref string path) { if (string.IsNullOrWhiteSpace(path)) { path = $"{GetSafeFileNameWithoutExtension(inputPath)}.png"; } if (!Path.HasExtension(path)) { path += ".png"; } if (path.Any(x => Path.GetInvalidPathChars().Contains(x))) { throw new ParameterValidationException("The output path is invalid."); } if (File.Exists(path) && shouldOverwriteOutput(path) == false) { throw new OperationCanceledException(); } } ValidateOutputPath(ref outputPath); string smoothedOutputPath = null; if (generateSmoothVersion) { var name = $"{GetSafeFileNameWithoutExtension(outputPath)}_smoothed{Path.GetExtension(outputPath)}"; smoothedOutputPath = Path.Combine(Path.GetDirectoryName(outputPath), name); ValidateOutputPath(ref smoothedOutputPath); } if (!int.TryParse(rawBarWidth, out var barWidth) || barWidth <= 0) { throw new ParameterValidationException("Invalid bar width."); } if (!int.TryParse(rawImageWidth, out var imageWidth) || imageWidth <= 0) { throw new ParameterValidationException("Invalid output width."); } int?imageHeight = null; if (!useInputHeightForOutput) { if (int.TryParse(rawImageHeight, out var nonNullableImageHeight) && nonNullableImageHeight > 0) { imageHeight = nonNullableImageHeight; } else { throw new ParameterValidationException("Invalid output height."); } } var barcodeParameters = new BarCodeParameters() { BarWidth = barWidth, Width = imageWidth, Height = imageHeight, }; return(new CompleteBarCodeGenerationParameters { BarCode = barcodeParameters, InputPath = inputPath, OutputPath = outputPath, SmoothedOutputPath = smoothedOutputPath, GenerateSmoothedOutput = generateSmoothVersion, }); }