Search code examples
c#.netxnaawesomium

Is there a way to use AwesomiumSharp to create a System.Drawing.Image in memory?


Background is, I'm using XNA, and I render Awesomium to an Image, which I then make a Texture2D from.

The code to render Awesomium to an Image via a file looks something like this:

webView.Render().SaveToPNG("awesomium.png", true);
var image = Image.FromFile("awesomium.png", true);

Which works fine, but it's dog slow (as you can imagine).

Is there a way to use Awesomium to render to a System.Drawing.Image without writing out to the filesystem?


Solution

  • In the end I found my answer in awesomiumdotnet. I guess the official wrapper isn't always the most complete :/

    public static class Rbex
    {
        public static Bitmap ToBitmap(this RenderBuffer buffer)
        {
            const int depth = 4; 
            const PixelFormat pf = PixelFormat.Format32bppArgb;
    
            // Create bitmap
            Bitmap bitmap = new Bitmap(buffer.GetWidth(), buffer.GetHeight(), pf);
    
            BitmapData data = bitmap.LockBits(new Rectangle(0,0, buffer.GetWidth(), buffer.GetHeight()), ImageLockMode.WriteOnly, bitmap.PixelFormat);
    
            buffer.CopyTo(data.Scan0, buffer.GetWidth() * depth, depth, false);
            bitmap.UnlockBits(data);
    
            return bitmap;
        }
    }