将Surface保存到位图并在C#中优化DirectX屏幕捕获

Ale*_*lex 13 c# optimization image save slimdx

经过一整天的测试后,我想出了这个代码,它使用DirectX(SlimDX)捕获当前屏幕并将其保存到文件中:

Device d;

public DxScreenCapture()
{
    PresentParameters present_params = new PresentParameters();
    present_params.Windowed = true;
    present_params.SwapEffect = SwapEffect.Discard;
    d = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.SoftwareVertexProcessing, present_params);
}

public Surface CaptureScreen()
{
    Surface s = Surface.CreateOffscreenPlain(d, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Format.A8R8G8B8, Pool.Scratch);
    d.GetFrontBufferData(0, s);
    return s;
}
Run Code Online (Sandbox Code Playgroud)

然后我做以下事情:

   DxScreenCapture sc = new DxScreenCapture();
Run Code Online (Sandbox Code Playgroud)

..代码在这里

    private void button1_Click(object sender, EventArgs e)
    {

        Stopwatch stopwatch = new Stopwatch();

        // Begin timing
        stopwatch.Start();

        Surface s = sc.CaptureScreen();
        Surface.ToFile(s, @"c:\temp\test.png", ImageFileFormat.Png);

        s.Dispose();

        stopwatch.Stop();

        textBox1.Text = ("Elapsed:" + stopwatch.Elapsed.TotalMilliseconds);
    }
Run Code Online (Sandbox Code Playgroud)

结果是:

0.当我不保存表面时:平均.经过时间:80-90ms

1.当我还将Surface保存到BMP文件时:格式:ImageFileFormat.Bmp,avg.经过时间:120毫秒,文件大小:7mb

2.当我还将Surface保存到PNG文件时:格式:ImageFileFormat.Png,avg.经过时间:800毫秒,文件大小:300kb

问题是:

1.是否可以优化当前的图像捕获?根据这篇文章 - Directx屏幕截图应该比GDI更快.对我来说,GDI通常需要20ms来获得"位图",而使用DX获得"Surfare"需要80ms(两者都没有保存).

http://www.codeproject.com/Articles/274461/Very-fast-screen-capture-using-DirectX-in-Csharp

2A.如何更快地将Surface保存为PNG图像格式?当我将表面保存到7mb BMP文件时,它比将同一表面保存到300kb PNG文件所花费的时间少了近6倍.

2B.是否可以将Surface直接保存到Bitmap,这样我就不必创建临时文件了?

所以我不必做以下事情:Surface - > image file; 图像文件打开 - >位图; ,而是:Surface - >位图

目前为止就这样了.我很乐意接受任何提示,谢谢!

编辑:

刚刚解决了2b:

Bitmap bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(s, SlimDX.Direct3D9.ImageFileFormat.Bmp));
Run Code Online (Sandbox Code Playgroud)

EDIT2:

Surface.ToFile(s, @"C:\temp\test.bmp", ImageFileFormat.Bmp);
Bitmap bitmap = new Bitmap(@"C:\temp\test.bmp");
Run Code Online (Sandbox Code Playgroud)

比以下更快:

Bitmap bitmap = new Bitmap(SlimDX.Direct3D9.Surface.ToStream(s, SlimDX.Direct3D9.ImageFileFormat.Bmp));
Run Code Online (Sandbox Code Playgroud)

100毫秒!是的,我也不敢相信我的眼睛;)我不喜欢临时文件创建的想法,但50%的性能提升(100-200ms而不是200-300 +)是一件非常好的事情.

小智 1

如果你不想使用 SlimDX 库你也可以尝试

public Bitmap GimmeBitmap(Surface s)
{
    GraphicsStream gs = SurfaceLoader.SaveToStream(ImageFileFormat.Bmp, s);
    return new Bitmap(gs);
}
Run Code Online (Sandbox Code Playgroud)

并尝试对 .png 进行相同的操作 - 我没有测试性能,但它必须比使用光盘临时文件更快:)

至于第一个问题 - 尝试仅创建一次表面,然后在每个屏幕截图上仅将设备的缓冲区数据放入其中并创建位图

d.GetFrontBufferData(0, s);
return new Bitmap(SurfaceLoader.SaveToStream(ImageFileFormat.Bmp, s));
Run Code Online (Sandbox Code Playgroud)

这应该可以节省你一些时间:)