Since the algorithm I am using accepts System.Drawing.Bitmap
I only needed a way to capture a rendered frame onto one and then render the result, this is far from efficient but I've got a solution.
The solution uses PrintWindow from the WIN API.
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
This would probably only work if an embedded SharpDX control is used as capturing the control via handle won't do, but capturing a parent control, in this case the form, will return the desired image.
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
IntPtr hdcBitmap = g.GetHdc();
PrintWindow(Handle, hdcBitmap, 0);
g.ReleaseHdc(hdcBitmap);
g.Dispose();
// Crop the image to return only the part that we need, without control borders.
bmp.Clone(new Rectangle(SharpDXControl.Location.X + 1, SharpDXControl.Location.Y + 1, SharpDXControl.Width - 2, SharpDXControl.Height - 2), bmp.PixelFormat);
To draw the image, I created a new SharpDX.Direct2D1.Bitmap
instance and used the pixel data (bits) from the System.Drawing.Bitmap
instance.
SharpDX.Direct2D1.Bitmap dxbmp = new SharpDX.Direct2D1.Bitmap(renderTarget, new SharpDX.Size2(bmpWidth, bmpHeight), new BitmapProperties(renderTarget.PixelFormat));
dxbmp.CopyFromMemory(bmpBits, bmpWidth * 4);
renderTarget.DrawBitmap(dxbmp, 1F, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
dxbmp.Dispose();
The algorithm I am using is a slightly modified version of the Queue-Linear Flood Fill Algorithm.