点击或拖拽改变大小

HtmlRender 类

Standalone static class for simple and direct HTML rendering.
For WinForms UI prefer using HTML controls: [!:HtmlPanel] or [!:DSkinHtmlLabel].
For low-level control and performance consider using HtmlContainer.
继承层次
SystemObject
  DSkin.HtmlHtmlRender

命名空间:  DSkin.Html
程序集:  DSkin (在 DSkin.dll 中) 版本:16.6.3.22
语法
C#
public static class HtmlRender

HtmlRender 类型公开以下成员。

方法
  名称说明
公共方法静态成员AddFontFamily
Adds a font family to be used in html rendering.
The added font will be used by all rendering function including HtmlContainer and all WinForms controls.
公共方法静态成员AddFontFamilyMapping
Adds a font mapping from fromFamily to toFamily iff the fromFamily is not found.
When the fromFamily font is used in rendered html and is not found in existing fonts (installed or added) it will be replaced by toFamily.
公共方法静态成员Measure
Measure the size (width and height) required to draw the given html under given max width restriction.
If no max width restriction is given the layout will use the maximum possible width required by the content, it can be the longest text line or full image width.
Use GDI text rendering, note TextRenderingHint has no effect.
公共方法静态成员MeasureGdiPlus
Measure the size (width and height) required to draw the given html under given max width restriction.
If no max width restriction is given the layout will use the maximum possible width required by the content, it can be the longest text line or full image width.
Use GDI+ text rending, use TextRenderingHint to control text rendering.
公共方法静态成员ParseStyleSheet
Parse the given stylesheet to CssData object.
If combineWithDefault is true the parsed css blocks are added to the default css data (as defined by W3), merged if class name already exists. If false only the data in the given stylesheet is returned.
公共方法静态成员Render(Graphics, String, PointF, SizeF, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML source on the specified location and max size restriction.
Use GDI text rendering, note TextRenderingHint has no effect.
If maxSize.Width is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
If 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.
Returned is the actual width and height of the rendered html.
公共方法静态成员Render(Graphics, String, Single, Single, Single, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML source on the specified location and max width restriction.
Use GDI text rendering, note TextRenderingHint has no effect.
If maxWidth is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
Returned is the actual width and height of the rendered html.
公共方法静态成员RenderGdiPlus(Graphics, String, PointF, SizeF, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML source on the specified location and max size restriction.
Use GDI+ text rending, use TextRenderingHint to control text rendering.
If maxSize.Width is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
If 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.
Returned is the actual width and height of the rendered html.
公共方法静态成员RenderGdiPlus(Graphics, String, Single, Single, Single, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML source on the specified location and max size restriction.
Use GDI+ text rending, use TextRenderingHint to control text rendering.
If maxWidth is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
Returned is the actual width and height of the rendered html.
公共方法静态成员RenderToImage(Image, String, PointF, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML on top of the given image.
image will contain the rendered html in it on top of original content.
image must not contain transparent pixels as it will corrupt the rendered html text.
The HTML will be layout by the given image size but may be clipped if cannot fit.
See "Rendering to image" remarks section on HtmlRender.
公共方法静态成员RenderToImage(String, Size, Color, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML into a new image of the requested size.
The HTML will be layout by the given size but will be clipped if cannot fit.

Limitation: The image cannot have transparent background, by default it will be white.
See "Rendering to image" remarks section on HtmlRender.

公共方法静态成员RenderToImage(Image, String, PointF, SizeF, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML on top of the given image.
image will contain the rendered html in it on top of original content.
image must not contain transparent pixels as it will corrupt the rendered html text.
See "Rendering to image" remarks section on HtmlRender.
公共方法静态成员RenderToImage(String, Size, Size, Color, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML into a new image of unknown size that will be determined by min/max width/height and HTML layout.
If maxSize.Width.Width is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
If maxSize.Height.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.
If minSize (Width/Height) is above zero the rendered image will not be smaller than the given min size.

Limitation: The image cannot have transparent background, by default it will be white.
See "Rendering to image" remarks section on HtmlRender.

公共方法静态成员RenderToImage(String, Int32, Int32, Color, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML into a new image of unknown size that will be determined by max width/height and HTML layout.
If maxWidth is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
If 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.

Limitation: The image cannot have transparent background, by default it will be white.
See "Rendering to image" remarks section on HtmlRender.

公共方法静态成员RenderToImageGdiPlus(String, Size, TextRenderingHint, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML into a new image of the requested size.
The HTML will be layout by the given size but will be clipped if cannot fit.
The generated image have transparent background that the html is rendered on.
GDI+ text rending can be controlled by providing TextRenderingHint.
See "Rendering to image" remarks section on HtmlRender.
公共方法静态成员RenderToImageGdiPlus(String, Size, Size, TextRenderingHint, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML into a new image of unknown size that will be determined by min/max width/height and HTML layout.
If maxSize.Width.Width is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
If maxSize.Height.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.
If minSize (Width/Height) is above zero the rendered image will not be smaller than the given min size.
The generated image have transparent background that the html is rendered on.
GDI+ text rending can be controlled by providing TextRenderingHint.
See "Rendering to image" remarks section on HtmlRender.
公共方法静态成员RenderToImageGdiPlus(String, Int32, Int32, TextRenderingHint, CssData, EventHandlerHtmlStylesheetLoadEventArgs, EventHandlerHtmlImageLoadEventArgs)
Renders the specified HTML into a new image of unknown size that will be determined by max width/height and HTML layout.
If maxWidth is zero the html will use all the required width, otherwise it will perform line wrap as specified in the html
If 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.
The generated image have transparent background that the html is rendered on.
GDI+ text rending can be controlled by providing TextRenderingHint.
See "Rendering to image" remarks section on HtmlRender.
Top
备注

GDI vs. GDI+ text rendering
Windows supports two text rendering technologies: GDI and GDI+.
GDI is older, has better performance and looks better on standard monitors but doesn't support alpha channel for transparency.
GDI+ is newer, device independent so work better for printers but is slower and looks worse on monitors.
HtmlRender supports both GDI and GDI+ text rendering to accommodate different needs, GDI+ text rendering methods have "GdiPlus" suffix in their name where GDI do not.

Rendering to image
See https://htmlrenderer.codeplex.com/wikipage?title=Image%20generation
Because of GDI text rendering issue with alpha channel clear type text rendering rendering to image requires special handling.
Solid color background - generate an image where the background is filled with solid color and all the html is rendered on top of the background color, GDI text rendering will be used. (RenderToImage method where the first argument is html string)
Image background - render html on top of existing image with whatever currently exist but it cannot have transparent pixels, GDI text rendering will be used. (RenderToImage method where the first argument is Image object)
Transparent background - render html to empty image using GDI+ text rendering, the generated image can be transparent. Text rendering can be controlled using TextRenderingHint, note that ClearTypeGridFit doesn't render well on transparent background. (RenderToImageGdiPlus method)

Overwrite stylesheet resolution
Exposed by optional "stylesheetLoad" delegate argument.
Invoked when a stylesheet is about to be loaded by file path or URL in 'link' element.
Allows to overwrite the loaded stylesheet by providing the stylesheet data manually, or different source (file or URL) to load from.
Example: The stylesheet 'href' can be non-valid URI string that is interpreted in the overwrite delegate by custom logic to pre-loaded stylesheet object
If no alternative data is provided the original source will be used.

Overwrite image resolution
Exposed by optional "imageLoad" delegate argument.
Invoked when an image is about to be loaded by file path, URL or inline data in 'img' element or background-image CSS style.
Allows to overwrite the loaded image by providing the image object manually, or different source (file or URL) to load from.
Example: image 'src' can be non-valid string that is interpreted in the overwrite delegate by custom logic to resource image object
Example: image 'src' in the html is relative - the overwrite intercepts the load and provide full source URL to load the image from
Example: image download requires authentication - the overwrite intercepts the load, downloads the image to disk using custom code and provide file path to load the image from.
If no alternative data is provided the original source will be used.
Note: Cannot use asynchronous scheme overwrite scheme.

示例

Simple rendering
HtmlRender.Render(g, "<div>Hello <b>World</b></div>");
HtmlRender.Render(g, "<div>Hello <b>World</b></div>", 10, 10, 500, CssData.Parse("body {font-size: 20px}")");

Image rendering
HtmlRender.RenderToImage("<div>Hello <b>World</b></div>", new Size(600,400));
HtmlRender.RenderToImage("<div>Hello <b>World</b></div>", 600);
HtmlRender.RenderToImage(existingImage, "<div>Hello <b>World</b></div>");

参见