0

It's my first time posting here so apologies in advance if I get the code formatting wrong. I'm coding a simple GDI+ renderer class in C#. Here's a simplified version of my code:

public interface IRenderer
{
    void Begin();
    void ClearFrame(byte R, byte G, byte B);
    byte[] End();

}

public class RendererGDI : IRenderer
{
    List<Action> renderFunctions;
    Bitmap b = new Bitmap(256,256);
    Graphics g = Graphics.FromImage(b);

    public RendererGDI()
    {
        renderFunctions = new List<Action>();
    }

    void Begin()
    {
        renderFunctions.Clear();
    }

    void ClearFrame(byte R, byte G, byte B)
    {           
        renderFunctions.Add(() => g.Clear(Color.FromArgb(255, R,G,B)));
    }

    byte[] End()
    {
        foreach (Action func in renderFunctions)
            func();
    }
}

The End() function isn't fully implemented yet but I'm wondering if this is the correct way to store System.Drawing.Graphics operations and only have them execute inside the End() function?

Thanks very much for your time and help.

Craig.

  • Yes this would work, just remember to implement the IDisposable pattern, or to clean up all your resources in the end method. – TheGeneral Mar 31 '19 at 05:25
  • Awesome! Thank you very much! – Craig Berry Mar 31 '19 at 05:32
  • Will you be always drawing on a `256x256` Bitmap? Meaning, what is that pre-built Bitmap as a Field doing there? You should have means to *inherit* a Graphics object from a source unknown until run-time. Or provide a constructor/method/property that sets the Graphics object or a Bitmap, or creates one using some parameters. A List of Actions is used quite often when drawing shapes or other sequences of graphic objects, you just need to make you class more *generic*. – Jimi Mar 31 '19 at 05:41
  • @CraigBerry Consider using `Action` as signature for the renderer function: `List> renderFunctions`. You could then pass the class private `Graphics` instance when you call it inside the `End()` function, making the `Graphics` instance accessible to use within the renderer functions. – IronGeek Mar 31 '19 at 06:39
  • Both fantastic suggestions. Yeah the 256x256 Bitmap was just put in there for a quick example. I am making the recommended changes now. Thanks again guys! – Craig Berry Mar 31 '19 at 18:32

0 Answers0