HappyDude February 2016

Will using DirectX or OpenGL speed up 2D Rendering on a windows form?

I'm working on a structured light project where I need to project what are essentially barcodes at 60fps. These codes are built from bitmap images that are 1920x1200. When using GDI in C# I'm only getting around 19fps on a pretty beastly computer. I'm looking into SharpDX now and before investing a lot of time I was wondering if rendering the images to the screen via DirectX or OpenGL will actually be faster?

Sample code:

// Gets a reference to the current BufferedGraphicsContext
currentContext = BufferedGraphicsManager.Current;
List<Image> imagesGrayCode = new List<Image>();

Bitmap bitmap = (Bitmap)Image.FromFile("F:/LAB/Hardware Triggering Demo/Lib/Patterns_11bit_RLL.tiff");
int count = bitmap.GetFrameCount(FrameDimension.Page);

for (int idx = 0; idx < count; idx++)
{
    // save each frame to a bytestream
    bitmap.SelectActiveFrame(FrameDimension.Page, idx);
    MemoryStream byteStream = new MemoryStream();
    bitmap.Save(byteStream, ImageFormat.Tiff);

    // and then create a new Image from it
    imagesGrayCode.Add(Image.FromStream(byteStream));
    //this.progressBar1.Value = idx;
}


Thread.Sleep(1000);
stopwatch = Stopwatch.StartNew();
int acq_wait_time = 10;

for (int i = 0; i < 10; i++)
{
    foreach (Image img in imagesGrayCode)
    {
        myBuffer.Graphics.DrawImage(img, this.DisplayRectangle);
        // Renders the contents of the buffer to the drawing surface associated with the buffer.
        myBuffer.Render();
        // Renders the contents of the buffer to the specified drawing surface.
        myBuffer.Render(this.CreateGraphics());

        //System.Threading.Thread.Sleep(acq_wait_time);
        stopwatch.Reset();
        stopwatch.Start();
        while (stopwatch.ElapsedMilliseconds < acq_wait_time)
        { }
        FPS++;
        Application.DoEvents();
    }
}

Updated Code:

// Gets a reference to the current BufferedGraphicsContext
currentContex        

Answers


David Turnbull February 2016

You're drawing > rendering > drawing > rendering > drawing > rendering etc. You can do all the drawing ahead of time so you only need to render on each frame. Use more of whatever myBuffer is; one for each image.

Profiler tools are awesome for these problems. I'm guessing based on your partial code and my experience, but a profiler can tell you exactly how long each function takes to complete.

Post Status

Asked in February 2016
Viewed 1,824 times
Voted 8
Answered 1 times

Search




Leave an answer