如何从Surface(SharpDX)创建位图 [英] How to create bitmap from Surface (SharpDX)
问题描述
我是DirectX的新手,并尝试使用SharpDX通过桌面复制API捕获屏幕截图。
I am new to DirectX and trying to use SharpDX to capture a screen shot using the Desktop Duplication API.
我想知道是否有任何简单的方法可以创建可在CPU中使用的位图(即保存在文件等)
I am wondering if there is any easy way to create bitmap that I can use in CPU (i.e. save on file, etc.)
我正在使用以下代码获取桌面屏幕快照:
I am using the following code the get the desktop screen shot:
var factory = new SharpDX.DXGI.Factory1();
var adapter = factory.Adapters1[0];
var output = adapter.Outputs[0];
var device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware,
DeviceCreationFlags.BgraSupport |
DeviceCreationFlags.Debug);
var dev1 = device.QueryInterface<SharpDX.DXGI.Device1>();
var output1 = output.QueryInterface<Output1>();
var duplication = output1.DuplicateOutput(dev1);
OutputDuplicateFrameInformation frameInfo;
SharpDX.DXGI.Resource desktopResource;
duplication.AcquireNextFrame(50, out frameInfo, out desktopResource);
var desktopSurface = desktopResource.QueryInterface<Surface>();
任何人都可以给我一些有关如何从desktopSurface(DXGI)创建位图对象的想法。表面实例)?
can anyone please give me some idea on how can I create a bitmap object from the desktopSurface (DXGI.Surface instance)?
推荐答案
桌面复制API的MSDN页面告诉我们图像的格式:
The MSDN page for the Desktop Duplication API tells us the format of the image:
DXGI通过新的IDXGIOutputDuplication :: AcquireNextFrame方法提供了一个包含当前桌面图像的表面。无论当前的显示模式是什么,桌面映像的格式始终为 DXGI_FORMAT_B8G8R8A8_UNORM 。
您可以使用 Surface.Map(MapFlags,输出DataStream)
方法可以访问CPU上的数据。
You can use the Surface.Map(MapFlags, out DataStream)
method get access to the data on the CPU.
代码应如下所示:
DataStream dataStream;
desktopSurface.Map(MapFlags.Read, out dataStream);
for(int y = 0; y < surface.Description.Width; y++) {
for(int x = 0; x < surface.Description.Height; x++) {
// read DXGI_FORMAT_B8G8R8A8_UNORM pixel:
byte b = dataStream.Read<byte>();
byte g = dataStream.Read<byte>();
byte r = dataStream.Read<byte>();
byte a = dataStream.Read<byte>();
// color (r, g, b, a) and pixel position (x, y) are available
// TODO: write to bitmap or process otherwise
}
}
desktopSurface.Unmap();
*免责声明:我手头没有Windows 8安装,我只是在遵循文档。我希望这能奏效:)
*Disclaimer: I don't have a Windows 8 installation at hand, I'm only following the documentation. I hope this works :)
这篇关于如何从Surface(SharpDX)创建位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!