Exemplo n.º 1
0
        public void UpdatePagePrintSize(VisualPrintPreviewController controller, bool keepPageCellsSize = false)
        {
            if (controller == null)
            {
                return;
            }

            var pageSettings = printDocument.DefaultPageSettings;

            using (controller.SuspendUpdateVisualImage())
            {
                var pageSize = new Size(
                    pageSettings.Bounds.Width - pageSettings.Margins.Left - pageSettings.Margins.Right,
                    pageSettings.Bounds.Height - pageSettings.Margins.Top - pageSettings.Margins.Bottom);
                if (ForPdf)
                {
                    pageSize = new Size(pageSize.Width * 72 / 100, pageSize.Height * 72 / 100);
                }
                using (keepPageCellsSize ? controller.SuspendChangePageCellsSize() : null)
                {
                    controller.PagePrintSize = pageSize;
                    controller.PixelsPerMm   = (ForPdf ? 72m : 100m) / 25.4m;
                }
            }

            printDocument.PrinterSettings.MinimumPage = 1;
            printDocument.PrinterSettings.FromPage    = 1;
            printDocument.PrinterSettings.MaximumPage = controller.PagesCount;
            printDocument.PrinterSettings.ToPage      = controller.PagesCount;
        }
Exemplo n.º 2
0
 public EmfPainter(string fileName, IntPtr dc, Size imageSize, Func <IndexedImage, Bitmap> toBitmap) : base(null, toBitmap, false)
 {
     metafile    = new Metafile(fileName, dc, new Rectangle(0, 0, imageSize.Width, imageSize.Height), MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
     GdiGraphics = Graphics.FromImage(metafile);
 }
Exemplo n.º 3
0
        public void SaveToPdf()
        {
            ResetPreviewControl();
            using (var saveDialog = new SaveFileDialog {
                Filter = Resources.FileFilterPDF, FilterIndex = 0, FileName = PreselectedPdfFileName
            })
            {
                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    PreselectedPdfFileName = string.Empty;
                    var wasForPdf = ForPdf;

                    Cursor = Cursors.WaitCursor;
                    try
                    {
                        var document = new PdfDocument();
                        document.Info.Title            = Controller.SourceImage.Description;
                        document.Info.Author           = SystemInformation.UserName;
                        document.Info.Creator          = AppInfo.AppDescription + " (" + AppInfo.AppVersion + ")";
                        document.Info.CreationDate     = DateTime.Now;
                        document.Info.ModificationDate = document.Info.CreationDate;
                        document.Info.Subject          = Resources.SaeFileDescription;

                        var pageSettings = printDocument.DefaultPageSettings;
                        var pageMargins  = pageSettings.Margins;

                        var pageWidth  = new XUnit(pageSettings.PaperSize.Width * 72.0 / 100.0, XGraphicsUnit.Point);
                        var pageHeight = new XUnit(pageSettings.PaperSize.Height * 72.0 / 100.0, XGraphicsUnit.Point);
                        if (pageSettings.Landscape)
                        {
                            var x = pageWidth;
                            pageWidth  = pageHeight;
                            pageHeight = x;
                        }
                        var pageSize = new Size((int)pageWidth.Point, (int)pageHeight.Point);

                        var marginsRect = new Rectangle(
                            pageMargins.Left * 72 / 100,
                            pageMargins.Top * 72 / 100,
                            pageSize.Width - (pageMargins.Left + pageMargins.Right) * 72 / 100,
                            pageSize.Height - (pageMargins.Top + pageMargins.Bottom) * 72 / 100);

                        ForPdf = true;
                        UpdatePagePrintSize(Controller);

                        using (var form = new Form
                        {
                            Text = Resources.PrintSavingToPdf,
                            Size = new System.Drawing.Size(200, 130),
                            TopMost = true,
                            FormBorderStyle = FormBorderStyle.FixedDialog,
                            ShowIcon = false,
                            ControlBox = false
                        })
                        {
                            using (var painter = new PdfSharpPainter(IndexedImageExtensions.ToBitmap)
                            {
                                FontName = Controller.GridPainter.SymbolsFont.Name,
                                FontFamily = FontHelper.GetFontFamily(Controller.GridPainter.SymbolsFont.Name)
                            })
                            {
                                var progressLabel = new Label {
                                    Location = new System.Drawing.Point(32, 24), AutoSize = true
                                };
                                form.Controls.Add(progressLabel);
                                form.Show();

                                for (int i = 0; i < Controller.PagesCount; i++)
                                {
                                    progressLabel.Text = string.Format(Resources.PrintPageNo, i + 1, Controller.SourceImage.Description);
                                    form.Invalidate();
                                    form.Update();

                                    var page = document.AddPage();
                                    page.Orientation = pageSettings.Landscape ? PageOrientation.Landscape : PageOrientation.Portrait;
                                    page.Width       = pageWidth;
                                    page.Height      = pageHeight;

                                    using (var pdfGraphics = XGraphics.FromPdfPage(page))
                                    {
                                        painter.PdfGraphics = pdfGraphics;                                         // Keep same painter for different pages to share fonts

                                        Controller.PaintPrintPageImage(
                                            painter,
                                            i,
                                            pageSize,
                                            marginsRect,
                                            AppInfo.AppDescription,
                                            Controller.SourceImage.Description ?? Resources.UnsavedImageDescription);
                                    }
                                }
                            }

                            document.Save(saveDialog.FileName);
                        }

                        if (File.Exists(saveDialog.FileName))
                        {
                            Process.Start(saveDialog.FileName);
                        }

                        Dispose();
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(Resources.ErrorSavePdf + Environment.NewLine + ex.Message);
                        ForPdf = wasForPdf;
                        UpdatePagePrintSize(Controller);
                        Cursor = DefaultCursor;
                    }
                }
            }
        }
Exemplo n.º 4
0
        void InitializeRecentFilesButtons(ToolTip toolTip)
        {
            if (string.IsNullOrEmpty(Settings.Default.LastOpenFiles))
            {
                var startupPath = Path.Combine(AppInfo.StartupPath, "Samples");
                if (!string.IsNullOrEmpty(startupPath) && Directory.Exists(startupPath))
                {
                    var sampleFileNames = Directory.GetFiles(startupPath, "*.sa4").Take(10);
                    Settings.Default.LastOpenFiles = string.Join(Settings.FilesSeparator.ToString(), sampleFileNames);
                    Settings.Default.Save();
                }
            }

            foreach (var fileName in Settings.Default.LastOpenFiles.Split(Settings.FilesSeparator))
            {
                if (File.Exists(fileName))
                {
                    IndexedImage image;
                    try
                    {
                        using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                        {
                            image = ImageSerializer.LoadFromStream(stream);
                        }
                    }
                    catch (IOException)
                    {
                        continue;
                    }

                    if (image != null && image.Size.Width > 0 && image.Size.Height > 0)
                    {
                        Bitmap bitmap;
                        if (image.Size.Width > 200 || image.Size.Height > 200)
                        {
                            var maxLength = Math.Max(image.Size.Width, image.Size.Height);
                            var newSize   = new Size(image.Size.Width * 200 / maxLength, image.Size.Height * 200 / maxLength);
                            bitmap = new ImageResampler().Resample(image, newSize, ImageResampler.FilterType.Box).ToBitmap();
                        }
                        else
                        {
                            bitmap = image.ToBitmap();
                        }

                        var imageButton = new FlatButton();
                        imageButton.Size       = new System.Drawing.Size(250, 250);
                        imageButton.Image      = bitmap;
                        imageButton.Text       = Environment.NewLine + Path.GetFileNameWithoutExtension(fileName);
                        imageButton.Tag        = fileName;
                        imageButton.TextAlign  = ContentAlignment.BottomCenter;
                        imageButton.ImageAlign = ContentAlignment.MiddleCenter;
                        imageButton.FlatAppearance.BorderSize = 0;
                        imageButton.Click += ImageButton_Click;

                        var tooltip = fileName + Environment.NewLine +
                                      string.Format(Resources.ImageInfoTooltip, image.Size.Width, image.Size.Height, image.Palette.Count);
                        toolTip.SetToolTip(imageButton, tooltip);

                        panelLastOpenFiles.Controls.Add(imageButton);
                    }
                }
            }
        }