使用 Lockbits 搜索黑色像素,但是这是随机的吗?
我遇到了 Lockbit 问题。 我正在搜索黑色像素,因为它看起来不正确,所以我使用 WindowPopup 来打印每个像素的颜色,但这就像我的程序正在使用另一张图片。
替代文本 http://i208.photobucket.com/ albums/bb91/Savaronna/pixel-1.jpg?t=1234874238
我将第一个找到的黑色像素标记为红色。 正如您所看到的,还有其他几个像素也应该匹配。 我究竟做错了什么?
这是我的剧本,我需要监督什么吗?
Bitmap b = this.TableListBMP;
BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppPArgb);
int stride = bmpData.Stride;
IntPtr Scan0 = bmpData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = bmpData.Stride - b.Width*4;
int xOffset, yOffset;
for(int y=5; y<b.Height; ++y)
{
for(int x=1; x<b.Width; ++x)
{
MessageBox.Show(
string.Format("x={0}, y={1}, ARGB={2},{3},{4},{5}",
x, y,
Convert.ToString(p[(y*stride)+(x*4)]),
Convert.ToString(p[(y*stride)+(x*4)+1]),
Convert.ToString(p[(y*stride)+(x*4)+2]),
Convert.ToString(p[(y*stride)+(x*4)+3])));
p +=4;
}
// EDIT: This line should be removed
p += nOffset;
}
}
b.UnlockBits(bmpData);
I got a problem with Lockbits. I'm searching black pixels, and because it does not seem right, I'm using a WindowPopup to print the colors of every pixel, but it's like my program is using another picture.
alt text http://i208.photobucket.com/albums/bb91/Savaronna/pixel-1.jpg?t=1234874238
I marked the first found black pixel red. As you can see there are several other pixels that should match too. What am I doing wrong?
This is my script, do I oversee something?
Bitmap b = this.TableListBMP;
BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppPArgb);
int stride = bmpData.Stride;
IntPtr Scan0 = bmpData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = bmpData.Stride - b.Width*4;
int xOffset, yOffset;
for(int y=5; y<b.Height; ++y)
{
for(int x=1; x<b.Width; ++x)
{
MessageBox.Show(
string.Format("x={0}, y={1}, ARGB={2},{3},{4},{5}",
x, y,
Convert.ToString(p[(y*stride)+(x*4)]),
Convert.ToString(p[(y*stride)+(x*4)+1]),
Convert.ToString(p[(y*stride)+(x*4)+2]),
Convert.ToString(p[(y*stride)+(x*4)+3])));
p +=4;
}
// EDIT: This line should be removed
p += nOffset;
}
}
b.UnlockBits(bmpData);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用诸如
p[((y)*stride)+((x)*4)]
之类的偏移量,您可能根本不应该增加 p ? 您肯定已经用乘法处理过这个问题(x
和y
)吗? 另外,bmpData.Stride - b.Width*4 不是(据我所知)一个有意义的数字...步幅是包括填充在内的行大小。删除“
p += ...
”代码,或更改偏移算法。 目前,您正在跳过数据(不好)并访问对象外部的数据(非常不好)。If you are using offsets such as
p[((y)*stride)+((x)*4)]
, you probably shouldn't be increasing p at all? Surely you've already handled that (x
andy
) with the multiplication? Also,bmpData.Stride - b.Width*4
is not (as far as I can see) a meaningful number... the stride is the rows size including padding.Either remove the "
p += ...
" code, or change the offset algorithm. At the moment you are skipping data (bad) and accessing data outside of the object (very bad).