使用DirectX或OpenGL会加速Windows窗体上的2D渲染吗?

时间:2016-02-08 17:04:07

标签: c# opengl directx

我正在开发一个结构化的灯光项目,我需要以60fps的速度投影基本上是条形码的。这些代码是根据1920x1200的位图图像构建的。在C#中使用GDI时,我只能在相当野兽的计算机上获得大约19fps的速度。我现在正在研究SharpDX,在投入大量时间之前,我想知道是否通过DirectX或OpenGL将图像渲染到屏幕上实际上会更快?

示例代码:

// 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();
    }
}

更新代码:

// Gets a reference to the current BufferedGraphicsContext
currentContext = BufferedGraphicsManager.Current;
List<Image> imagesGrayCode = new List<Image>();
List<BufferedGraphics> ImageBuffers = new List<BufferedGraphics>();
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;
}

//create a buffer from each image in memory
for (int i = 0; i < imagesGrayCode.Count(); i++)
{
    ImageBuffers.Add(currentContext.Allocate(this.CreateGraphics(),this.DisplayRectangle));
    ImageBuffers.ElementAt(i).Graphics.DrawImage(imagesGrayCode.ElementAt(i), this.DisplayRectangle);
    ImageBuffers.ElementAt(i).Render();
}

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

for (int x = 0; x < 10; x++)
{
    //display image buffers sequentially
    for (int i = 0; i < imagesGrayCode.Count(); i++)
    {
        // Renders the contents of the buffer to the specified drawing surface.
        ImageBuffers.ElementAt(i).Render(this.CreateGraphics());

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

1 个答案:

答案 0 :(得分:2)

你正在画画&gt;渲染&gt;绘图&gt;渲染&gt;绘图&gt;您可以提前完成所有绘图,因此您只需要在每个帧上渲染。使用更多myBuffer;每个图像一个。

Profiler工具对于这些问题非常棒。我猜是基于你的部分代码和我的经验,但是一个分析器可以告诉你完全每个函数需要多长时间才能完成。