How To Render HTML Table on Bitmap

I have been trying to render HTML with tables on a Bitmap but till now got no success.

I have tried to load HTML in a WebView and then using view.Draw or CapturePicture() to get a screenshot but everytime I get a blank screenshot because OnPageFinished() is executing later.

My Application is like

Main Activity -> ViewPager Activity -> Adapter Populates with imageCreationClass.cs which uses following code to capture picture

ViewPager Activity Class

public class TreeCatalog
{
static TreePage[] treeBuiltInCatalog = {
    new TreePage {
        imageId = Resource.Drawable.larch,
        caption = "Introduction",
        bmp = BitmapFactory.DecodeFile(tempPath + "1.jpg")
    },

    new TreePage {
        imageId = Resource.Drawable.maple,
        caption = "Highlights",
        bmp = BitmapFactory.DecodeFile(tempPath + "2.jpg")
    },

     new TreePage {
        imageId = Resource.Drawable.maple,
        caption = "Illustration",
        bmp = BitmapFactory.DecodeFile(tempPath + "3.jpg")
     },             
};      

  private TreePage[] treePages;
  public TreeCatalog() { treePages = treeBuiltInCatalog; }
  public TreePage this[int i] { get { return treePages[i]; } }
  public int NumTrees { get { return treePages.Length; } }
    }

imageCreationClass.cs

public static void preparePageThree()
{
    WebView web = new WebView(logic.context);                        
    web.Layout(0, 0, 800, 800);
    web.SetWebViewClient(new MyWebViewClient());
    web.LoadData(HtmlSource, "text/html", "utf-8");
}


    public class MyWebViewClient : WebViewClient
    {
    public override bool ShouldOverrideUrlLoading(WebView view, string url)
    {
        view.LoadUrl(url);
        return true;
    }

    public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
    {
        base.OnPageStarted(view, url, favicon);
    }

    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);
        //Picture picture = view.CapturePicture();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.InMutable = true;

        Bitmap.Config conf = Bitmap.Config.Argb8888;
        bmp = Bitmap.CreateBitmap(200, 200, conf);
        Canvas canvas = new Canvas(bmp);
        //canvas.DrawColor(Color.Beige);
        view.Draw(canvas);
        //canvas.DrawPicture(picture);

        if(File.Exists(tempPath + "3.jpg"))
        {
            File.Delete(tempPath + "3.jpg");
        }

        using (var os = new System.IO.FileStream(logic.tempPath + "3.jpg", System.IO.FileMode.CreateNew))
        {
            bmp.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
        }                
    }

    public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl)
    {
        base.OnReceivedError(view, errorCode, description, failingUrl);
    }
  }

Please guide If there are better ways we can render HTML directly on image (Bitmap).

Sign In or Register to comment.