0

如何计算图形流内特定像素的位置?此流是我的主监视器的屏幕截图。假设我想要像素的位置:

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);
}

但这会返回数字从大约 53 000 到大约 7 000 000 的集合(来自另一种类似的方法)。提取该颜色后

Surface s = sc.CaptureScreen();
GraphicsStream gs = s.LockRectangle(LockFlags.None);

byte[] bu = new byte[4];    
foreach (long pos in positions)
{
    gs.Position = pos;
    gs.Read(bu, 0, 4);
    r += bu[2];
    g += bu[1];
    b += bu[0];
    i++;
}

我需要制作包含这些职位的自己的收藏。

4

2 回答 2

0

我假设您在此图形流 ( doc ) 中拥有屏幕截图的所有像素,并希望在指定像素位置 (x,y) 的流中获取索引。

因此它应该很容易:Index = (x + y * screenwidth) * SizeOf(Pixel)

要读取数据,您应该SeekRead.

于 2013-07-23T17:43:35.883 回答
0

好的,我找到了答案,它在我的代码中,我不完全明白为什么,但是:

//x - screen width
//y - screen height
Bpp = 4; // ?
gs.Position = (x * Screen.PrimaryScreen.Bounds.Width + y) * Bpp;
于 2013-07-24T10:46:55.920 回答