问题描述
我试图从 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 (将#修改为@)