Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.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# 如何计算屏幕上特定像素的流位置?_C#_Directx - Fatal编程技术网

C# 如何计算屏幕上特定像素的流位置?

C# 如何计算屏幕上特定像素的流位置?,c#,directx,C#,Directx,如何计算图形流中特定像素的位置?这个流是我的主监视器的屏幕截图。 假设我想要像素在以下位置的位置: 0, 0 0, 10 10, 0 10, 10 我正在使用DirectX 9 SDK。 以下是我用来计算位置的部分代码(来自教程): const int Bpp = 4; int o = 20; int m = 8; int screenWidthMinusM = Screen.PrimaryScreen.Bounds.Width - m; int screenHeightMinus

如何计算图形流中特定像素的位置?这个流是我的主监视器的屏幕截图。 假设我想要像素在以下位置的位置:

0, 0  
0, 10  
10, 0  
10, 10
我正在使用DirectX 9 SDK。
以下是我用来计算位置的部分代码(来自教程):

const int Bpp = 4;
int o = 20;
int m = 8;
int screenWidthMinusM = Screen.PrimaryScreen.Bounds.Width - m;
int screenHeightMinusM = Screen.PrimaryScreen.Bounds.Height - m;
int bx = (screenWidthMinusM - m) / 3 + m;
int by = (screenHeightMinusM - m) / 3 + m;
int bx2 = (screenWidthMinusM - m) * 2 / 3 + m;
int by2 = (screenHeightMinusM - m) * 2 / 3 + m;

long x, y;
long pos;

y = m;
for (x = m; x < screenWidthMinusM; x += o)
{
   pos = (y * Screen.PrimaryScreen.Bounds.Width + x) * Bpp;  
   if (x < bx) tlPos.Add(pos);
   else if (x > bx && x < bx2) tPos.Add(pos);
   else if (x > bx2) trPos.Add(pos);
}

我需要制作包含这些位置的我自己的集合。

我假设您的屏幕截图的所有像素都在这个图形流()中,并且希望获得指定像素位置(x,y)流中的索引

因此,它应该很容易:
Index=(x+y*屏幕宽度)*SizeOf(像素)


要阅读数据,您应该将
Seek
Read
结合起来。好的,我找到了答案,答案在我的代码中,我不太明白为什么,但是:

//x - screen width
//y - screen height
Bpp = 4; // ?
gs.Position = (x * Screen.PrimaryScreen.Bounds.Width + y) * Bpp;

我可能会犯错误(不知道什么是
图形流
),但这取决于使用的顶点着色器和
世界
视图
投影
矩阵。您可以指定您使用的DirectX和DirectX包装的版本吗?DX9和问题已更新,否:)假设屏幕宽度为1920,(0,10)将导致19200和(10,0)到10。最后一个问题是,屏幕1920x1080的最后一点是否为1919x1079?为什么1920x1079没有异常,但1920x1080返回异常“试图读取或写入受保护的内存。这通常表示其他内存已损坏。”?嗯,我认为您正在读取未分配的内存,但并非所有内存都受保护。所以很幸运,在1920x1079时没有抛出异常。这是我建议的(我忘记了像素大小),除了我认为宽度应该乘以y。Bpp是一个像素的大小,记住它是一个ByTestStream,每个像素由4个通道ARGB组成,每个通道ARGB有1个字节。
//x - screen width
//y - screen height
Bpp = 4; // ?
gs.Position = (x * Screen.PrimaryScreen.Bounds.Width + y) * Bpp;