public override void RenderObject()
        {
            Point anchor = new Point(0, 0);

            Font font = Fonts.GetFont(FontID, Zoom.GetFontSize(FontSize), (FontStyle)FontStyle);

            Graphics g = Graphics.FromHwnd(IntPtr.Zero);

            g.ApplyGraphicsQuality();

            // Measure label size
            SizeF s = g.MeasureString(ContentTable[Languages.UsedLanguages[0]], font);

            s = new SizeF((float)Math.Ceiling(s.Width), (float)Math.Ceiling(s.Height));

            g.Dispose();

            // Is Object Vertical
            if (IsVertical)
            {
                s.Flip();
            }

            Bitmap b = new Bitmap((int)s.Width, (int)s.Height);

            g = Graphics.FromImage(b);
            g.ApplyGraphicsQuality();

            Point trans = Point.Empty;
            int   angle = 0;

            if (IsVertical)
            {
                if (!IsTurned180)
                {
                    trans.X = (int)s.Width;
                }
                angle += 90;
            }

            if (IsTurned180)
            {
                if (!IsVertical)
                {
                    trans = new Point((int)s.Width, (int)s.Height);
                }
                else
                {
                    trans.Y = Funcs.ToInt(s.Height);
                }

                angle += 180;
            }

            // Apply transformation when label is rotated
            g.TranslateTransform(trans.X, trans.Y);
            g.RotateTransform(angle);

            // Draw the string
            g.DrawString(ContentTable[Languages.UsedLanguages[0]],
                         font,
                         new SolidBrush(TextColor),
                         Point.Empty);

            // Remove the transformation for next drawing methods
            g.ResetTransform();

            // Draw the anchor point for this label
            if (Config.ShowAnchorPoints)
            {
                if (IsCenter())
                {
                    anchor.X = (int)(s.Width * 0.5F);
                }
                if (IsRight())
                {
                    anchor.X = (int)s.Width - 1;
                }

                if (IsMiddle())
                {
                    anchor.Y = (int)(s.Height * 0.5F);
                }
                if (IsDown())
                {
                    anchor.Y = (int)s.Height - 1;
                }

                g.DrawLine(Config.AnchorPen, anchor.X - Config.ANCHOR_SIZE, anchor.Y, anchor.X + Config.ANCHOR_SIZE, anchor.Y);
                g.DrawLine(Config.AnchorPen, anchor.X, anchor.Y - Config.ANCHOR_SIZE, anchor.X, anchor.Y + Config.ANCHOR_SIZE);
            }

            // Release previous rendered bitmap (A lil bit of memore'h :3)
            if (Holder.Image != null)
            {
                Holder.Image.Dispose();
            }

            // Set Canvas Image
            Holder.Image = b;
            Holder.Size  = Holder.Image.Size;

            base.RenderObject();
        }
Exemple #2
0
        public void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            // we do not use the buffers when printing (the printer is probably higher-resolution!)
            Graphics gr   = e.Graphics;
            Page     page = CurrentDocument.Page(m_PrintPageIndex);

            e.PageSettings.Landscape = page.IsLandscape;
            gr.ResetTransform();
            gr.PageUnit = GraphicsUnit.Pixel;                                                    // GraphicsUnit.Millimeter ' it shows 600 DPI which makes the usual scaling conversion for mono impossible

            SizeF printable = e.PageSettings.PrintableArea.Size.MultiplyBy(Geometry.INCH / 100); // in mm.  Apparently e.MarginBounds or e.PageSettings.PrintableArea is in hundredths of an inch  (?)

            if (page.IsLandscape)
            {
                printable = printable.Flip();                 // But sadly e.PageSettings.PrintableArea does not account for orientation
            }
            var pageSize = page.Size;
            // max scale that will fit:
            var scale = Math.Min(printable.Width / pageSize.Width, printable.Height / pageSize.Height);

            if (scale > 0.97f)             // will not fit at full size (allow a bit <1 in case of slight margin issues)
            {
                scale = 1;                 // don't shrink
            }
            else
            {
                pageSize.Width  *= scale;                // update page size for centreing below
                pageSize.Height *= scale;
            }

            gr.ScaleTransform(scale * gr.DpiX / Geometry.INCH, scale * gr.DpiY / Geometry.INCH);
            gr.TranslateTransform(0, page.Size.Height - 1);
            // centre it
            gr.TranslateTransform(scale * (pageSize.Width - pageSize.Width) / 2, scale * (pageSize.Height - pageSize.Height) / 2);
            // No need to take account of the margins if we are centring it anyway
            //gr.TranslateTransform(m_objPage.Margin - INCH * e.PageSettings.HardMarginX / 100, m_objPage.Margin - INCH * e.PageSettings.HardMarginY / 100)
            gr.SmoothingMode     = SmoothingMode.AntiAlias;
            gr.TextRenderingHint = TextRenderingHint.AntiAlias;             // otherwise text on transparent buffer is naff

            using (NetCanvas canvas = new NetCanvas(gr, true))
            {
                page.DrawBackground(canvas, scale * gr.DpiX / Geometry.INCH, page.Paper.PrintBackground, false);
                if (page != CurrentPage)
                {
                    page.BackgroundImage?.Release();                     // avoid overload if document has masses of large pages
                }
                if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.Selection)
                {
                    page.DrawSelected(canvas, null, scale, gr.DpiX / Geometry.INCH, true);
                }
                else
                {
                    page.DrawShapes(canvas, scale, gr.DpiX / Geometry.INCH, null, !PrintMeasures);
                }
            }

            m_PrintPageIndex += 1;
            switch (m_PrintDocument.PrinterSettings.PrintRange)
            {
            case PrintRange.CurrentPage:
            case PrintRange.Selection:
                e.HasMorePages = false;
                break;

            default:
                e.HasMorePages = m_PrintPageIndex + 1 <= m_PrintDocument.PrinterSettings.ToPage;                         // +1 is the conversion from 0-based to 1-based
                break;
            }
        }