我正在使用 捕获屏幕SharpDX,处理更改并复制到暂存纹理以便稍后保存。
当源屏幕旋转(纵向)时,源纹理不会旋转(文档也这么说)。如何快速旋转纹理并放入我的暂存纹理?
这是我的代码(暂时忽略 Moved Rectangles 部分,稍后我将不得不应用相同的内容):
using (var screenTexture = resource.QueryInterface<Texture2D>())
{
#region Moved rectangles (ignore it for now)
var movedRectangles = new OutputDuplicateMoveRectangle[info.TotalMetadataBufferSize];
DuplicatedOutput.GetFrameMoveRects(movedRectangles.Length, movedRectangles, out var movedRegionsLength);
for (var movedIndex = 0; movedIndex < movedRegionsLength / Marshal.SizeOf(typeof(OutputDuplicateMoveRectangle)); movedIndex++)
{
//Crop the destination rectangle to the screen area rectangle.
var left = Math.Max(movedRectangles[movedIndex].DestinationRect.Left, Left);
var right = Math.Min(movedRectangles[movedIndex].DestinationRect.Right, Left + Width);
var top = Math.Max(movedRectangles[movedIndex].DestinationRect.Top, Top);
var bottom = Math.Min(movedRectangles[movedIndex].DestinationRect.Bottom, Top + Height);
//Copies from the screen texture only the area which the user wants to capture.
if (right > left && bottom > top)
{
//Limit the source rectangle to the available size within the destination rectangle.
var sourceWidth = movedRectangles[movedIndex].SourcePoint.X + (right - left);
var sourceHeight = movedRectangles[movedIndex].SourcePoint.Y + (bottom - top);
Device.ImmediateContext.CopySubresourceRegion(screenTexture, 0, new ResourceRegion(movedRectangles[movedIndex].SourcePoint.X, movedRectangles[movedIndex].SourcePoint.Y, 0, sourceWidth, sourceHeight, 1),
BackingTexture, 0, left - Left, top - Top);
}
}
#endregion
#region Dirty rectangles
var dirtyRectangles = new RawRectangle[info.TotalMetadataBufferSize];
DuplicatedOutput.GetFrameDirtyRects(dirtyRectangles.Length, dirtyRectangles, out var dirtyRegionsLength);
for (var dirtyIndex = 0; dirtyIndex < dirtyRegionsLength / Marshal.SizeOf(typeof(RawRectangle)); dirtyIndex++)
{
//OffsetTop and OffsetLeft are the bounds of the screen
//(where the screen is located within the virtual space).
//Top and Left is the capture position.
//Crop screen positions and size to frame sizes.
var left = Math.Max(dirtyRectangles[dirtyIndex].Left, Left - OffsetLeft);
var right = Math.Min(dirtyRectangles[dirtyIndex].Right, Left + Width - OffsetLeft);
var top = Math.Max(dirtyRectangles[dirtyIndex].Top, Top - OffsetTop);
var bottom = Math.Min(dirtyRectangles[dirtyIndex].Bottom, Top + Height - OffsetTop);
//Copies from the screen texture only the area which the user wants to capture.
if (right > left && bottom > top)
Device.ImmediateContext.CopySubresourceRegion(screenTexture, 0, new ResourceRegion(left, top, 0, right, bottom, 1), BackingTexture, 0, left - (Left - OffsetLeft), top - (Top - OffsetTop));
}
#endregion
}
据我了解,dirtyRectangles数组给出的界限和screenTexture被旋转了。
获得正确位置后,我必须先旋转纹理,然后再尝试复制到我的BackingTexture.
我BackingTexture的创建是这样的:
BackingTexture = new Texture2D(Device, new Texture2DDescription
{
ArraySize = 1,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
Format = Format.B8G8R8A8_UNorm,
Width = Width,
Height = Height,
OptionFlags = ResourceOptionFlags.None,
MipLevels = 1,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default
});