Exemplo n.º 1
0
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (this.metafiles.Count < 1)
            {
                // no metafiles to show - do nothing
                return;
            }

            // set the smoothing mode so that the rendering looks good
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            var  brush_black      = System.Drawing.Brushes.Black;
            var  brush_gray       = System.Drawing.Brushes.Gray;
            var  brush_white      = System.Drawing.Brushes.White;
            var  pen_red          = System.Drawing.Pens.Red;
            var  pen_green        = System.Drawing.Pens.Green;
            var  renderbox_margin = new SD.Size(10, 10);
            bool use_print_area   = this.checkBoxUsePrintableArea.Checked;

            // retrieve the default size to be used for printing
            var printer_settings = this.GetCurentPrinterSettings();
            var landscape        = printer_settings.DefaultPageSettings.Landscape;

            // Get the sizes in common units
            SD.SizeF      papersize_raw      = printer_settings.DefaultPageSettings.PaperSize.ToSizeF();
            SD.SizeF      rdlsize_raw        = this.rdlmd.PageSize;
            SD.RectangleF printable_area_raw = printer_settings.DefaultPageSettings.PrintableArea;

            // Normalize the sizes into comparable units of 100th of an inch
            SD.SizeF      papersize_normalized      = landscape ? papersize_raw.FlipSides() : papersize_raw; // adjust for landscape
            SD.RectangleF printable_rect_normalized = landscape ? printable_area_raw.FlipSides() : printable_area_raw;
            SD.SizeF      rdlsize_normalized        = rdlsize_raw.MultiplyBy(100.0f);                        // multiply by 100 to match printer units

            // Fit the normalized RDL into the papersize
            SD.SizeF rdlsize_normalized_size = use_print_area
                                                   ? rdlsize_normalized.ResizeDownToFit(printable_rect_normalized.Size)
                                                   : rdlsize_normalized.ResizeDownToFit(papersize_normalized);

            // the renderbox is the full area dedicated to the preview
            // renderbox_margin keeps the preview from bumping into the edges of the renderbox
            var renderbox_rect = this.panelRenderBox.Size.ToRect();

            // display rect is the actual available region we use to render the preview
            var display_rect = renderbox_rect.GetInternalRectangle(renderbox_margin);



            // Coords for the white area, accounting for Landscape
            var paper_rect = papersize_normalized.ToRectF();

            var paper_scale = SSRSCommon.GraphicsUtil.GetFitToAreaScalingFactor(paper_rect.Size, display_rect.Size);

            int new_paper_x = (int)((display_rect.Width - ((int)paper_rect.Width * paper_scale)) / 2.0);
            int new_paper_y = (int)((display_rect.Height - ((int)paper_rect.Height * paper_scale)) / 2.0);


            // Coords for the design
            var mf = this.metafiles[this.page_index];


            SD.RectangleF rdl_rect = use_print_area
                                       ? rdlsize_normalized_size.ToRectF().Add(printable_rect_normalized.Location)
                                       : rdlsize_normalized_size.ToRectF().Add(0.0f, 0.0f);


            // do the drawing -----------------------------------------

            // Erase the entire background
            e.Graphics.FillRectangle(brush_gray, renderbox_rect);

            // Translate to account for the renderbox margin

            e.Graphics.TranslateTransform(renderbox_margin.Width + new_paper_x, renderbox_margin.Height + new_paper_y);
            e.Graphics.ScaleTransform((float)paper_scale, (float)paper_scale);

            // Draw a white rectangle to simulate the physical page used by the printer
            e.Graphics.FillRectangle(brush_white, paper_rect);



            // Draw annotations
            bool debug_rendering           = true;
            bool debug_show_printable_area = true;
            bool debug_show_design_edges   = true;
            bool debug_show_inch_grid      = this.checkBoxGrid.Checked;
            bool debug_show_halfinch_grid  = this.checkBoxGridHalf.Checked;

            if (debug_rendering)
            {
                if (debug_show_inch_grid)
                {
                    draw_inch_grid(e, papersize_normalized, 1.0f);
                }
                if (debug_show_halfinch_grid)
                {
                    draw_inch_grid(e, papersize_normalized, 0.5f);
                }
                if (debug_show_printable_area)
                {
                    e.Graphics.DrawRectangle(pen_green, printable_rect_normalized); // Green the printable area
                }
                if (debug_show_design_edges)
                {
                    // RED - the edges of the RDL design
                    e.Graphics.DrawRectangle(pen_red, rdl_rect);
                }
            }

            // Draw the EMF into a region on the white rectangle
            e.Graphics.DrawImage(mf, rdl_rect);
        }