Пример #1
0
        private void browseOutput_Click(object sender, EventArgs e)
        {
            _UpdateEffectivePath( );
            string forecastFilename = m_Wp.GetFullOutputPathForPage(1);

            var sfd = new SaveFileDialog( );

            sfd.InitialDirectory = outputPath.Text;
            sfd.FileName         = forecastFilename;
            sfd.DefaultExt       = GetUsingExt( );
            sfd.Filter           = "Image file|*.*";
            sfd.ValidateNames    = false;
            sfd.CheckFileExists  = false;
            sfd.CheckPathExists  = true;
            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog( ) == DialogResult.OK)
            {
                outputPath.Text = Path.GetDirectoryName(sfd.FileName);
                _UpdateEffectivePath( );
            }
        }
Пример #2
0
        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( );
        }