2017-06-28 7 views
0

C#でHTMLコードから画像を取得しようとしていますが、画像が取得できませんでした。 HTMLコードをC#コードの画像に変換する方法を教えてください。HTMLコードを画像に変換する方法C#の背後にあるコードを使用する

私はビットマップにhtmlコード文字列を渡してみましたが、私はこれを取得していません。

mailtxt = "<html><body><table><tr><td>Hello Sir</td></tr></table></body></html>"; 
StringBuilder str = new StringBuilder();str.Append(mailtxt.ToString()); 
Bitmap objBmpImage = new Bitmap(2, 2); 

     int intWidth = 0; 
     int intHeight = 0; 

     // Create the Font object for the image text drawing. 
     System.Drawing.Font objFont = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular, 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(str.ToString(), objFont).Width; 
     intHeight = (int)objGraphics.MeasureString(str.ToString(), 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(System.Drawing.Color.White); 
     objGraphics.SmoothingMode = SmoothingMode.HighQuality; 



     objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; // <-- This is the correct value to use. ClearTypeGridFit is better yet! 
     objGraphics.DrawString(str.ToString(), objFont, new SolidBrush(System.Drawing.Color.Black), 0, 0, StringFormat.GenericDefault); 

     objGraphics.Flush(); 

ありがとうございます。

+0

"objBmpImage''をコンストラクタに渡す理由は何ですか? '' objBmpImage =新しいビットマップ(objBmpImage、新しいサイズ(intWidth、intHeight)); ''あなたは完全に新しいものを作成しませんか? –

+0

ちょうど好奇心から、 " ..."などの文字列を含む画像が表示され、ブラウザで表示されるようなレンダリングされたHTMLは表示されません。それはあなたが欲しいものですか?または、htmlをレンダリングしますか? –

+0

返信ありがとうRand Random.Iレンダリングされたhtmlで画像が必要です。 – Naganath

答えて

0
public void ConvertHtmlToImage() 
{ 
    Bitmap m_Bitmap = new Bitmap(400, 600); 
    PointF point = new PointF(0, 0); 
    SizeF maxSize = new System.Drawing.SizeF(500, 500); 
    HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap), 
              "<html><body><p>This is a just a html code</p>" 
              + "<p>This is another html line</p></body>", 
              point, maxSize); 

    m_Bitmap.Save(@"C:\Test.png", ImageFormat.Png); 
} 
関連する問題