private void _UpdateEffectivePath( ) { outputPath.ReadOnly = sameOutputSourceOption.Checked; browseOutput.Enabled = !sameOutputSourceOption.Checked; if (sameOutputSourceOption.Checked) { if (!string.IsNullOrEmpty(inputPath.Text)) { outputPath.Text = Path.GetDirectoryName(inputPath.Text); } else { outputPath.Text = ""; } } // Filename decision is moved to WorkerParams if (m_Wp == null) { m_Wp = new WorkerParams( ); } m_Wp.inputFile = inputPath.Text; m_Wp.outputDir = outputPath.Text; m_Wp.outputPrefix = GetFilenamePrefix( ); m_Wp.outputExt = GetUsingExt( ); m_Wp.dpi = GetUsingDpi( ); m_Wp.outputFormat = (OutputFormat)formatSelect.SelectedIndex; if (string.IsNullOrEmpty(outputPath.Text)) { outputPathPreview.Text = "Select input file"; } else { outputPathPreview.Text = m_Wp.GetFullOutputPathForPage(1); } }
static void _ConvertImpl(BackgroundWorker worker, DoWorkEventArgs e) { WorkerParams wp = e.Argument as WorkerParams; int dpi = wp.dpi; int quality = 100; switch (wp.outputFormat) { case OutputFormat.Jpeg80: quality = 80; break; case OutputFormat.Jpeg50: quality = 50; break; } var jpegEncoder = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid); var jpegParams = new EncoderParameters(1); jpegParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); try { using (var document = PdfDocument.Load(wp.inputFile)) { var pageCount = document.PageCount; for (int i = 0; i < pageCount; i++) { string outputPath = wp.GetFullOutputPathForPage(i); worker.ReportProgress(0, $"Page {i} to\n{outputPath}"); using (Image image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi)) { switch (wp.outputFormat) { case OutputFormat.Jpeg80: case OutputFormat.Jpeg50: image.Save(outputPath, jpegEncoder, jpegParams); break; default: case OutputFormat.Png: image.Save(outputPath, ImageFormat.Png); break; } } if (worker.CancellationPending) { e.Cancel = true; break; } } } } catch (Exception ex) { e.Result = ex.Message; } jpegParams.Dispose( ); }