Esempio n. 1
0
        public static LayoutBitmap ConvertElementToBitmap(LayoutElement le, string filename)
        {
            if (le is LayoutBitmap)
            {
                return(null);
            }
            int padding = le is LayoutLegend ? 10 : 0;

            int width  = Convert.ToInt32(le.SizeF.Width * 3 + 0.5) + padding;
            int height = Convert.ToInt32(le.SizeF.Height * 3 + 0.5);

            if (le is LayoutMap)
            {
                width  = Convert.ToInt32(width * ScreenHelper.LogicToScreenDpi);
                height = Convert.ToInt32(height * ScreenHelper.LogicToScreenDpi);
            }

            using (var temp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
            {
                temp.SetResolution(96, 96);
                temp.MakeTransparent();

                using (var g = Graphics.FromImage(temp))
                {
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.ScaleTransform(300F / 100F, 300F / 100F);

                    if (!(le is LayoutMap))
                    {
                        g.TranslateTransform(-le.LocationF.X, -le.LocationF.Y);
                    }

                    le.DrawElement(g, false, false);
                }

                temp.SetResolution(300, 300);
                temp.Save(filename);
            }

            var bmp = new LayoutBitmap
            {
                Rectangle   = le.Rectangle,
                Name        = le.Name,
                Filename    = filename,
                Initialized = true
            };

            return(bmp);
        }
        /// <summary>
        /// Draws a single element of the layout.
        /// </summary>
        private void DrawElement(Graphics g, LayoutElement le, Rectangle invalRect, int index)
        {
            bool panning = _mouseMode == MouseMode.PanMap && IsSelected(index) && le is LayoutMap &&
                           _selectedLayoutElements.Count == 1;

            if (!panning && DrawMapResizing(index, g))
            {
                return;
            }

            if (le is LayoutMap)
            {
                var pnt = PaperToScreen(le.LocationF);

                g.TranslateTransform(Convert.ToInt32(pnt.X), Convert.ToInt32(pnt.Y));

                if (panning)
                {
                    // setting clip to the original rectangle
                    var r = PaperToScreen(le.Rectangle);
                    g.SetClip(new RectangleF(0f, 0f, r.Width, r.Height));

                    pnt.X += _mouseBox.Width;
                    pnt.Y += _mouseBox.Height;

                    // translating to new origin of buffer
                    g.ResetTransform();
                    g.TranslateTransform(Convert.ToInt32(pnt.X), Convert.ToInt32(pnt.Y));
                }

                g.ScaleTransform(_zoom, _zoom);
            }
            else
            {
                g.TranslateTransform(_paperLocation.X, _paperLocation.Y);

                g.ScaleTransform(ScreenHelper.LogicToScreenDpi * _zoom, ScreenHelper.LogicToScreenDpi * _zoom);
            }

            le.DrawElement(g, false, false);

            g.ResetClip();
            g.ResetTransform();
            g.TranslateTransform(-invalRect.X, -invalRect.Y);
        }