Home Ask Login Register

Developers Planet

Your answer is one click away!

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
    //this.progressBar1.Value = idx;

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.
        // Renders the contents of the buffer to the specified drawing surface.

        while (stopwatch.ElapsedMilliseconds < acq_wait_time)
        { }

Updated Code:

// Gets a reference to the current BufferedGraphicsContext


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


Leave an answer

Quote of the day: live life