Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SharpDX捕获旋转监视器的屏幕截图_C#_Directx_Sharpdx - Fatal编程技术网

C# 使用SharpDX捕获旋转监视器的屏幕截图

C# 使用SharpDX捕获旋转监视器的屏幕截图,c#,directx,sharpdx,C#,Directx,Sharpdx,下面的代码成功地截取了连接到我的Windows 10笔记本电脑的显示器的屏幕截图,只要显示器没有“翻转”。当监视器翻转到除“横向”以外的任何方向时,捕获的图像都是黑色像素(r、g、b、a=0,0,0255) 如何修改下面的代码,使其也能与翻转的监视器一起工作 目标框架是: 净额4.8 参考包包括: 夏普DX 4.2.0 SharpDX.Direct2D1 4.2.0 SharpDX.Direct3D11 4.2.0 SharpDX.DXGI 4.2.0 SharpDX.DXGI 4.2.0

下面的代码成功地截取了连接到我的Windows 10笔记本电脑的显示器的屏幕截图,只要显示器没有“翻转”。当监视器翻转到除“横向”以外的任何方向时,捕获的图像都是黑色像素(r、g、b、a=0,0,0255)

如何修改下面的代码,使其也能与翻转的监视器一起工作

目标框架是:

  • 净额4.8
参考包包括:

  • 夏普DX 4.2.0
  • SharpDX.Direct2D1 4.2.0
  • SharpDX.Direct3D11 4.2.0
  • SharpDX.DXGI 4.2.0
  • SharpDX.DXGI 4.2.0
使用系统;
使用System.IO;
使用System.Runtime.ExceptionServices;
使用SharpDX;
名称空间屏幕广播{
内部静态类程序{
[状态线程]
私有静态void Main(){
var captureCount=0;
使用var factory=new SharpDX.DXGI.Factory4();
foreach(工厂中的var适配器。适配器1){
使用var设备=新的SharpDX.Direct3D11.device(适配器);
foreach(adapter.Outputs中的var输出){
if(output.Description.IsAttachedToDesktop){
var description=output.description;
使用var output1=output.QueryInterface();
捕获($“{captureCount++}.bmp”,设备,输出1);
}
output.Dispose();
}
adapter.Dispose();
}
}
私有静态无效捕获(字符串输出文件名,SharpDX.Direct3D11.Device设备,SharpDX.DXGI.Output1输出1){
int width=output1.Description.DesktopBounds.Right-output1.Description.DesktopBounds.Left;
int height=output1.Description.DesktopBounds.Bottom-output1.Description.DesktopBounds.Top;
使用var stagingScreenTexture=new SharpDX.Direct3D11.Texture2D(设备,new SharpDX.Direct3D11.texture2dddescription{
宽度=宽度,
高度=高度,
CpuAccessFlags=SharpDX.Direct3D11.CpuAccessFlags.Read,
BindFlags=SharpDX.Direct3D11.BindFlags.None,
格式=SharpDX.DXGI.Format.B8G8R8A8_UNorm,
OptionFlags=SharpDX.Direct3D11.ResourceOptionFlags.None,
MIP级别=1,
ArraySize=1,
SampleDescription={Count=1,Quality=0},
用法=SharpDX.Direct3D11.resourceausage.Staging
});
使用var duplicatedOutput=output1.DuplicateOutput(设备);
SharpDX.DXGI.Resource screenResource=null;
SharpDX.DXGI.OutputDuplicateFrameInformation duplicateFrameInformation;
AcquireFrame(duplicatedOutput,输出duplicateFrameInformation,输出screenResource);
重复输出。释放帧();
AcquireFrame(duplicatedOutput,输出duplicateFrameInformation,输出screenResource);
//将资源复制到CPU可以访问的内存中
使用var screenTexture=screenResource.QueryInterface();
device.ImmediateContext.CopyResource(screenTexture、stagingScreenTexture);
//获取桌面捕获纹理
var mapSource=device.ImmediateContext.MapSubresource(stagingScreenTexture,0,SharpDX.Direct3D11.MapMode.Read,SharpDX.Direct3D11.MapFlags.None);
使用var bmp=new System.Drawing.Bitmap(宽度、高度、System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var bmpBounds=新系统.Drawing.Rectangle(0,0,宽度,高度);
var bmpData=bmp.LockBits(bmpBounds,System.Drawing.Imaging.ImageLockMode.WriteOnly,bmp.PixelFormat);
var src=mapSource.DataPointer;
var dest=bmpData.Scan0;
对于(变量y=0;y
我无法确定您是否引用了swapchain的backbuffer。当监视器旋转时,交换链有可能发生变化。所以你失去了对交换链的引用。那么,也许你复制了被破坏的表面。我不能确定你是否有一个关于swapchain的backbuffer的参考。当监视器旋转时,交换链有可能发生变化。所以你失去了对交换链的引用。然后,也许你们复制了被破坏的表面。