Ejemplo n.º 1
0
        /// <summary>
        /// Measure the size (width and height) required to draw the given html under given width and height restrictions.<br/>
        /// </summary>
        /// <param name="g">Device to use for measure</param>
        /// <param name="html">HTML source to render</param>
        /// <param name="maxWidth">optional: bound the width of the html to render in (default - 0, unlimited)</param>
        /// <param name="maxHeight">optional: bound the height of the html to render in (default - 0)</param>
        /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="bridge">optional: used to resolve external references in html code (property, method calls)</param>
        /// <returns>the size required for the html</returns>
        public static SizeF Measure(Graphics g, string html, float maxWidth = 0, float maxHeight = 0, CssData cssData = null, object bridge = null)
        {
            ArgChecker.AssertArgNotNull(g, "g");

            SizeF actualSize = SizeF.Empty;
            if (!string.IsNullOrEmpty(html))
            {
                var container = new HtmlContainer(html, bridge, cssData);
                container.MaxSize = new SizeF(maxWidth, maxHeight);
                container.PerformLayout(g);
                actualSize = container.ActualSize;
            }
            return actualSize;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Measure the size (width and height) required to draw the given html under given width and height restrictions.<br/>
 /// </summary>
 /// <param name="g">Device to use for measure</param>
 /// <param name="html">HTML source to render</param>
 /// <param name="maxWidth">optional: bound the width of the html to render in (default - 0, unlimited)</param>
 /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
 /// <returns>the size required for the html</returns>
 public static SizeF Measure(Graphics g, string html, float maxWidth = 0, CssData cssData = null)
 {
     ArgChecker.AssertArgNotNull(g, "g");
     return Measure(g, html, maxWidth, cssData, null, null);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Renders the specified HTML into image of unknown size that will be determined by max width/height and HTML layout.<br/>
        /// If <paramref name="maxWidth"/> is zero the html will use all the required width, otherwise it will perform line 
        /// wrap as specified in the html<br/>
        /// If <paramref name="maxHeight"/> is zero the html will use all the required height, otherwise it will clip at the
        /// given max height not rendering the html below it.<br/>
        /// </summary>
        /// <param name="html">HTML source to render</param>
        /// <param name="maxWidth">the max width of the rendered html</param>
        /// <param name="maxHeight">optional: the max height of the rendered html, if above zero it will be clipped</param>
        /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the generated image of the html</returns>
        public static Image RenderToImage(string html, int maxWidth, int maxHeight = 0, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
        {
            if (string.IsNullOrEmpty(html))
                return new Bitmap(0, 0, PixelFormat.Format32bppArgb);

            using (var htmlContainer = new HtmlContainer())
            {
                // use desktop created graphics to measure the HTML
                using (var measureGraphics = Graphics.FromHwnd(IntPtr.Zero))
                {
                    if (stylesheetLoad != null)
                        htmlContainer.StylesheetLoad += stylesheetLoad;
                    if (imageLoad != null)
                        htmlContainer.ImageLoad += imageLoad;
                    htmlContainer.SetHtml(html, cssData);

                    htmlContainer.PerformLayout(measureGraphics);

                    if (maxWidth > 0 && maxWidth < htmlContainer.ActualSize.Width)
                    {
                        // to allow the actual size be smaller than max we need to set max size only if it is really larger
                        htmlContainer.MaxSize = new SizeF(maxWidth, 0);
                        htmlContainer.PerformLayout(measureGraphics);
                    }
                }

                var size = new Size(maxWidth > 0 ? Math.Min(maxWidth, (int) htmlContainer.ActualSize.Width) : (int) htmlContainer.ActualSize.Width,
                                    maxHeight > 0 ? Math.Min(maxHeight, (int) htmlContainer.ActualSize.Height) : (int) htmlContainer.ActualSize.Height);

                // create the final image to render into by measured size
                var image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);

                // create memory buffer from desktop handle that supports alpha chanel
                IntPtr dib;
                var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib);
                try
                {
                    // render HTML into the memory buffer
                    using (var memoryGraphics = Graphics.FromHdc(memoryHdc))
                    {
                        memoryGraphics.Clear(Color.White);
                        htmlContainer.PerformPaint(memoryGraphics);
                    }

                    // copy from memory buffer to image
                    using (var imageGraphics = Graphics.FromImage(image))
                    {
                        var imgHdc = imageGraphics.GetHdc();
                        Win32Utils.BitBlt(imgHdc, 0, 0, image.Width, image.Height, memoryHdc, 0, 0, Win32Utils.BitBltCopy);
                        imageGraphics.ReleaseHdc(imgHdc);
                    }
                }
                finally
                {
                    Win32Utils.ReleaseMemoryHdc(memoryHdc, dib);
                }

                return image;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Renders the specified HTML into image of the requested size.<br/>
        /// The HTML will be layout by the given size but will be clipped if cannot fit.
        /// </summary>
        /// <param name="html">HTML source to render</param>
        /// <param name="size">The size of the image to render into, layout html by width and clipped by height</param>
        /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the generated image of the html</returns>
        public static Image RenderToImage(string html, Size size, CssData cssData = null, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = null, EventHandler<HtmlImageLoadEventArgs> imageLoad = null)
        {
            // create the final image to render into
            var image = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);

            if (!string.IsNullOrEmpty(html))
            {
                // create memory buffer from desktop handle that supports alpha chanel
                IntPtr dib;
                var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, image.Width, image.Height, out dib);
                try
                {
                    // create memory buffer graphics to use for HTML rendering
                    using (var memoryGraphics = Graphics.FromHdc(memoryHdc))
                    {
                        memoryGraphics.Clear(Color.White);

                        // render HTML into the memory buffer
                        using (var htmlContainer = new HtmlContainer())
                        {
                            if (stylesheetLoad != null)
                                htmlContainer.StylesheetLoad += stylesheetLoad;
                            if (imageLoad != null)
                                htmlContainer.ImageLoad += imageLoad;
                            htmlContainer.SetHtml(html, cssData);
                            htmlContainer.MaxSize = size;
                            htmlContainer.PerformLayout(memoryGraphics);
                            htmlContainer.PerformPaint(memoryGraphics);
                        }
                    }

                    // copy from memory buffer to image
                    using (var imageGraphics = Graphics.FromImage(image))
                    {
                        var imgHdc = imageGraphics.GetHdc();
                        Win32Utils.BitBlt(imgHdc, 0, 0, image.Width, image.Height, memoryHdc, 0, 0, Win32Utils.BitBltCopy);
                        imageGraphics.ReleaseHdc(imgHdc);
                    }
                }
                finally
                {
                    Win32Utils.ReleaseMemoryHdc(memoryHdc, dib);
                }
            }

            return image;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Renders the specified HTML source on the specified location and max size restriction.<br/>
        /// If <paramref name="maxSize"/>.Width is zero the html will use all the required width, otherwise it will perform line 
        /// wrap as specified in the html<br/>
        /// If <paramref name="maxSize"/>.Height is zero the html will use all the required height, otherwise it will clip at the
        /// given max height not rendering the html below it.<br/>
        /// Returned is the actual widht and height of the rendered html.<br/>
        /// </summary>
        /// <param name="g">Device to render with</param>
        /// <param name="html">HTML source to render</param>
        /// <param name="location">the top-left most location to start render the html at</param>
        /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param>
        /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the actual size of the rendered html</returns>
        public static SizeF Render(Graphics g, string html, PointF location, SizeF maxSize, CssData cssData, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad, EventHandler<HtmlImageLoadEventArgs> imageLoad)
        {
            ArgChecker.AssertArgNotNull(g, "g");

            SizeF actualSize = SizeF.Empty;
            if (!string.IsNullOrEmpty(html))
            {
                Region prevClip = null;
                if (maxSize.Height > 0)
                {
                    prevClip = g.Clip;
                    g.SetClip(new RectangleF(location, maxSize));
                }

                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                        container.StylesheetLoad += stylesheetLoad;
                    if (imageLoad != null)
                        container.ImageLoad += imageLoad;
                    container.SetHtml(html, cssData);
                    container.Location = location;
                    container.MaxSize = maxSize;
                    container.PerformLayout(g);
                    container.PerformPaint(g);

                    if (prevClip != null)
                    {
                        g.SetClip(prevClip, CombineMode.Replace);
                    }

                    actualSize = container.ActualSize;
                }
            }

            return actualSize;
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Renders the specified HTML source on the specified location and max size restriction.<br/>
 /// If <paramref name="maxSize"/>.Width is zero the html will use all the required width, otherwise it will perform line 
 /// wrap as specified in the html<br/>
 /// If <paramref name="maxSize"/>.Height is zero the html will use all the required height, otherwise it will clip at the
 /// given max height not rendering the html below it.<br/>
 /// Returned is the actual widht and height of the rendered html.<br/>
 /// </summary>
 /// <param name="g">Device to render with</param>
 /// <param name="html">HTML source to render</param>
 /// <param name="location">the top-left most location to start render the html at</param>
 /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param>
 /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
 /// <returns>the actual size of the rendered html</returns>
 public static SizeF Render(Graphics g, string html, PointF location, SizeF maxSize, CssData cssData = null)
 {
     ArgChecker.AssertArgNotNull(g, "g");
     return Render(g, html, location, maxSize, cssData, null, null);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Renders the specified HTML source on the specified location and max size restriction.<br/>
 /// If <paramref name="maxWidth"/> is zero the html will use all the required width, otherwise it will perform line 
 /// wrap as specified in the html<br/>
 /// Returned is the actual widht and height of the rendered html.<br/>
 /// </summary>
 /// <param name="g">Device to render with</param>
 /// <param name="html">HTML source to render</param>
 /// <param name="left">optional: the left most location to start render the html at (default - 0)</param>
 /// <param name="top">optional: the top most location to start render the html at (default - 0)</param>
 /// <param name="maxWidth">optional: Width to fit HTML drawing (default - 0, unlimited)</param>
 /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
 /// <returns>the actual size of the rendered html</returns>
 public static SizeF Render(Graphics g, string html, float left = 0, float top = 0, float maxWidth = 0, CssData cssData = null)
 {
     return Render(g, html, new PointF(left, top), new SizeF(maxWidth, 0), cssData, null, null);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Measure the size (width and height) required to draw the given html under given width and height restrictions.<br/>
        /// </summary>
        /// <param name="g">Device to use for measure</param>
        /// <param name="html">HTML source to render</param>
        /// <param name="maxWidth">optional: bound the width of the html to render in (default - 0, unlimited)</param>
        /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="stylesheetLoad">optional: can be used to overwrite stylesheet resolution logic</param>
        /// <param name="imageLoad">optional: can be used to overwrite image resolution logic</param>
        /// <returns>the size required for the html</returns>
        public static SizeF Measure(Graphics g, string html, float maxWidth, CssData cssData, EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad, EventHandler<HtmlImageLoadEventArgs> imageLoad)
        {
            ArgChecker.AssertArgNotNull(g, "g");

            SizeF actualSize = SizeF.Empty;
            if (!string.IsNullOrEmpty(html))
            {
                using (var container = new HtmlContainer())
                {
                    if (stylesheetLoad != null)
                        container.StylesheetLoad += stylesheetLoad;
                    if (imageLoad != null)
                        container.ImageLoad += imageLoad;
                    container.SetHtml(html, cssData);
                    container.MaxSize = new SizeF(maxWidth, 0);
                    container.PerformLayout(g);
                    actualSize = container.ActualSize;
                }
            }
            return actualSize;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Create deep copy of the css data with cloned css blocks.
 /// </summary>
 /// <returns>cloned object</returns>
 public CssData Clone()
 {
     var clone = new CssData();
     foreach (var mid in _mediaBlocks)
     {
         var cloneMid = new Dictionary<string, List<CssBlock>>();
         foreach (var blocks in mid.Value)
         {
             var cloneList = new List<CssBlock>();
             foreach (var cssBlock in blocks.Value)
             {
                 cloneList.Add(cssBlock.Clone());
             }
             cloneMid[blocks.Key] = cloneList;
         }
         clone._mediaBlocks[mid.Key] = cloneMid;
     }
     return clone;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Init with optinals document and stylesheet.
        /// </summary>
        /// <param name="htmlSource">the html to init with, init empty if not given</param>
        /// <param name="bridge">used to resolve external references in html code (property, method calls)</param>
        /// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
        public HtmlContainer(string htmlSource, object bridge, CssData baseCssData = null)
        {
            ArgChecker.AssertArgNotNullOrEmpty(htmlSource, "htmlSource");

            _bridge = bridge;
            _cssData = baseCssData ?? CssUtils.DefaultCssData;

            if(htmlSource != null)
            {
                _root = DomParser.GenerateCssTree(htmlSource, ref _cssData, bridge);
                if (_root != null)
                {
                    _root.HtmlContainer = this;
                    _selectionHandler = new SelectionHandler(_root);
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        private void Dispose(bool all)
        {
            try
            {
                if (all)
                {
                    LinkClicked = null;
                    Refresh = null;
                    RenderError = null;
                    StylesheetLoad = null;
                    ImageLoad = null;
                }

                _cssData = null;
                if (_root != null)
                    _root.Dispose();
                _root = null;
                if (_selectionHandler != null)
                    _selectionHandler.Dispose();
                _selectionHandler = null;
            }
            catch
            {
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Init with optional document and stylesheet.
        /// </summary>
        /// <param name="htmlSource">the html to init with, init empty if not given</param>
        /// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
        public void SetHtml(string htmlSource, CssData baseCssData = null)
        {
            if(_root != null)
            {
                _root.Dispose();
                _root = null;
                if (_selectionHandler != null)
                    _selectionHandler.Dispose();
                _selectionHandler = null;
            }

            if (!string.IsNullOrEmpty(htmlSource))
            {
                _cssData = baseCssData ?? CssUtils.DefaultCssData;

                _root = DomParser.GenerateCssTree(htmlSource, this, ref _cssData);
                if (_root != null)
                {
                    _selectionHandler = new SelectionHandler(_root);
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Renders the specified HTML source on the specified location and max size restriction.<br/>
        /// If <paramref name="maxSize"/>.Width is zero the html will use all the required width, otherwise it will perform line 
        /// wrap as specified in the html<br/>
        /// If <paramref name="maxSize"/>.Height is zero the html will use all the required height, otherwise it will clip at the
        /// given max height not rendering the html below it.<br/>
        /// Returned is the actual widht and height of the rendered html.<br/>
        /// </summary>
        /// <param name="g">Device to render with</param>
        /// <param name="html">HTML source to render</param>
        /// <param name="location">the top-left most location to start render the html at</param>
        /// <param name="maxSize">the max size of the rendered html (if height above zero it will be clipped)</param>
        /// <param name="cssData">optiona: the style to use for html rendering (default - use W3 default style)</param>
        /// <param name="bridge">optional: used to resolve external references in html code (property and method calls)</param>
        /// <returns>the actual size of the rendered html</returns>
        public static SizeF Render(Graphics g, string html, PointF location, SizeF maxSize, CssData cssData = null, object bridge = null)
        {
            ArgChecker.AssertArgNotNull(g, "g");

            SizeF actualSize = SizeF.Empty;
            if(!string.IsNullOrEmpty(html))
            {
                Region prevClip = null;
                if (maxSize.Height > 0)
                {
                    prevClip = g.Clip;
                    g.SetClip(new RectangleF(location, maxSize));
                }

                var container = new HtmlContainer(html, bridge, cssData);

                container.Location = location;
                container.MaxSize = maxSize;
                container.PerformLayout(g);
                container.PerformPaint(g);

                if (prevClip != null)
                {
                    g.SetClip(prevClip, CombineMode.Replace);
                }

                actualSize = container.ActualSize;
            }

            return actualSize;
        }