SharpDX를 사용하여 스크린 샷을 캡처하여 Windows Form (또는 패널)에 표시하려고하는데 문제가 있습니다. 을 사용하여 스크린 샷을 캡처 OutputDuplication.AcquireNextFrame
했지만 캡처 된 프레임을 사용하려고 WindowRenderTarget.DrawBitmap
하면 WrongResourceDomain 예외 와 함께 실패합니다 . Form을 추가하고 ScreenCapture 샘플 의 코드를 수정하여 문제를 설명했습니다.
using System;
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
namespace MiniTri
{
/// <summary>
/// Screen capture of the desktop using DXGI OutputDuplication.
/// </summary>
internal static class Program
{
[STAThread]
private static void Main()
{
var Form1 = new ScreenCapture.Form1();
// # of graphics card adapter
const int numAdapter = 0;
// # of output device (i.e. monitor)
const int numOutput = 0;
// Create DXGI Factory1
var dxgiFactory = new SharpDX.DXGI.Factory1();
var dxgiAdapter = dxgiFactory.GetAdapter1(numAdapter);
// Create device from Adapter
var d3dDevice = new SharpDX.Direct3D11.Device(dxgiAdapter, DeviceCreationFlags.BgraSupport);
// Create Direct2D1 Factory1
var d2dFactory = new SharpDX.Direct2D1.Factory1();
// Create Direct2D device
SharpDX.Direct2D1.Device d2dDevice;
using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>())
d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);
// Get the Direct2D1 DeviceContext
var d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None);
// Create Direct2D1 WindowRenderTarget
var WindowRenderTarget = new WindowRenderTarget(d2dFactory,
new RenderTargetProperties
{
Type = RenderTargetType.Default
},
new HwndRenderTargetProperties
{
Hwnd = Form1.Handle,
PixelSize = new Size2(Form1.Width, Form1.Height),
PresentOptions = PresentOptions.Immediately
});
// Get DXGI.Output
var output = dxgiAdapter.GetOutput(numOutput);
var output1 = output.QueryInterface<Output1>();
// Create RawRectangleF with the Form size
var FormRectangle = new SharpDX.Mathematics.Interop.RawRectangleF(0, 0, Form1.Width, Form1.Height);
// Duplicate the output
var duplicatedOutput = output1.DuplicateOutput(d3dDevice);
bool captureDone = false;
for (int i = 0; !captureDone; i++)
{
try
{
SharpDX.DXGI.Resource screenResource;
OutputDuplicateFrameInformation duplicateFrameInformation;
// Try to get duplicated frame within given time
duplicatedOutput.AcquireNextFrame(10000, out duplicateFrameInformation, out screenResource);
if (i > 0)
{
// Create a Direct2D1 Bitmap1 from screenResource
var dxgiSurface = screenResource.QueryInterface<Surface>();
var d2dBitmap1 = new Bitmap1(d2dContext, dxgiSurface);
// Should draw the bitmap on the form, but fails with "WrongResourceDomain" exception
WindowRenderTarget.BeginDraw();
WindowRenderTarget.DrawBitmap(d2dBitmap1, FormRectangle, 1.0f, BitmapInterpolationMode.Linear);
WindowRenderTarget.EndDraw();
// Capture done
captureDone = true;
}
screenResource.Dispose();
duplicatedOutput.ReleaseFrame();
}
catch (SharpDXException e)
{
if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
{
throw e;
}
}
}
// TODO: We should cleanp up all allocated COM objects here
}
}
}
프레임을 System.Drawing.Bitmap
(원본 샘플에서와 같이) 로 변환 한 다음 사용 System.Drawing.Graphics.DrawImage
하여 양식에 비트 맵을 그릴 수 있다는 것을 알고 있지만 이것을 피하고 SharpDX를 사용하여 프레임을 그리고 싶습니다. 나는 또한 ( MiniRect 샘플 과 유사 함)을 SwapChain
사용하여 프레임 을 만들고 그리 려고 시도했지만 동일한 예외로 실패합니다. 사용하여 동일한 . 본인은 여기에 이 예외 수단은 내가 오전 것을 다른 자원 도메인의 자원을 혼합 ,하지만 난 내 코드에서 뜻을 정확히 이해하지 않습니다. DirectX 사용 경험이 없습니다.Direct2D1.RenderTarget.DrawBitmap
Direct2D1.DeviceContext.DrawBitmap
대신를 SwapChain
다시 사용해 보았고 WindowRenderTarget
이번에는 성공했습니다.
SwapChainDescription
대상 컨트롤 (예 : 폼 또는 패널)의 특정 너비, 높이 및 핸들을 만듭니다 .SwapChain
사용하여 새로 만듭니다 .dxgiFactory
d3dDevice
SwapChainDescription
back buffer
의를 SwapChain
.DXGI Surface
가져옵니다 back buffer
.(기타의 생성에 대해 SwapChain
, back buffer
그리고 DXGI Surface
에 MiniRect 샘플 )
d2dContext
만들 Direct2D1.Bitmap1
로부터를 DXGI Surface
.d2dContext
다른 파일을 만듭니다 .Direct2D1.Bitmap1
screenResource
Direct2D1.Bitmap1
를 d2dContext
.d2dContext
두 번째 그리기를 사용하십시오 Direct2D1.Bitmap1
.SwapChain
.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다