我想在Asp.Net MVC中使用 C# 从 Textarea 或其他输入控件(不是整个网页)中输入的 HTML 代码生成图像(JPEG)。MVC 不支持 WebBrowser 控件。
此 HTML 包含数据和图像
HTML
<b>Convert Html To Image<b>
<br/>
<img src="C:\Logo.jpg" alt="Smiley face" width="42" height="42">
我将 html 代码传递给了位图图像创建功能的控制器
private Bitmap CreateBitmapImage(string html)
{
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
// Create the Font object for the image text drawing.
Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
// Create a graphics object to measure the text's width and height.
Graphics objGraphics = Graphics.FromImage(objBmpImage);
// This is where the bitmap size is determined.
intWidth = (int)objGraphics.MeasureString(html, objFont).Width;
intHeight = (int)objGraphics.MeasureString(html, objFont).Height;
// Create the bmpImage again with the correct size for the text and font.
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
// Add the colors to the new bitmap.
objGraphics = Graphics.FromImage(objBmpImage);
// Set Background color
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.DrawString(html, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
objGraphics.Flush();
return (objBmpImage);
}
但它会像这样返回输出
但需要输出
有没有办法生成这个(从 html 生成图像的步骤)或任何可用的第三方开源组件,如HtmlToImageConverter?