如何解决如何从 Texture2D 获取原始像素数据?
我试图从 SharpDX Texture2D 获取像素(rgba)值,然后将它们分配给位图,但我得到了这个错误的图像:
我知道问题是当我从 srcPtr
读取值时(见下面的代码),我认为偏移量是错误的...
这是代码:
SharpDX.dxgi.Resource screenResource;
OutputDuplicateFrameinformation duplicateFrameinformation;
// Try to get duplicated frame within given time is ms
duplicatedOutput.AcquireNextFrame(5,out duplicateFrameinformation,out screenResource);
// copy resource into memory that can be accessed by the cpu
using (var screenTexture2D = screenResource.QueryInterface<Texture2D>())
device.ImmediateContext.copyResource(screenTexture2D,screenTexture);
// Get the desktop capture texture
var mapSource = device.ImmediateContext.MapSubresource(screenTexture,MapMode.Read,SharpDX.Direct3D11.MapFlags.None);
// Create Drawing.Bitmap
using (var bitmap = new Bitmap(width,height,PixelFormat.Format32bppArgb))
{
var boundsRect = new Rectangle(0,width,height);
// copy pixels from screen capture Texture to GDI bitmap
var mapDest = bitmap.LockBits(boundsRect,ImageLockMode.ReadOnly,bitmap.PixelFormat);
unsafe
{
byte* srcPtr = (byte*)mapSource.DataPointer;
byte* destPtr = (byte*)mapDest.Scan0;
var curRowOffs = 0;
var stride = width * 4;
byte[] rgbaValues = new byte[width*height*4];
for (uint y = 0; y < height; y++)
{
var index = curRowOffs;
for (uint x = 0; x < width; x++)
{
// ARGB = bytes [B,G,R,A]
rgbaValues[index] = srcPtr[index];
rgbaValues[index + 1] = srcPtr[index + 1];
rgbaValues[index + 2] = srcPtr[index + 2];
rgbaValues[index + 3] = srcPtr[index + 3];
//copy rgba to bitmap
destPtr[index + 3] = srcPtr[index + 3];
destPtr[index + 2] = srcPtr[index + 2];
destPtr[index + 1] = srcPtr[index + 1];
destPtr[index] = srcPtr[index];
index += 4;
}
// Increase row offset
curRowOffs += stride;
}
}
// Release source and dest locks
bitmap.UnlockBits(mapDest);
device.ImmediateContext.UnmapSubresource(screenTexture,0);
我在这里做错了什么? 如何将原始像素数据放入字节数组?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。