2
\$\begingroup\$

I am working on a simple drawing game and I need to get a Flood Fill algorithm working for a Paint Bucket tool.

I have the game running on WinForms at the moment which is extremely slow, but I have a working Flood Fill algorithm there, I could get the code to support SharpDX if I could get the color of a pixel (either from a SharpDX.Direct2D1.Bitmap or from a SharpDX.Direct2D1.RenderTarget), but I can't find a way to do so.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.