C# 如何在每像素1位位图上正确记忆Marshal.ReadByte

C# 如何在每像素1位位图上正确记忆Marshal.ReadByte,c#,mouseevent,bitmapdata,C#,Mouseevent,Bitmapdata,我正在努力改进一个1bpp的图像搜索功能,我把它放在一起 我参考了鲍伯·鲍威尔的优秀页面,以及这里的一些答案,我能够整理出这个非常有效的例程 使用系统图; 使用系统、绘图、成像; 使用System.Runtime.InteropServices; 使用System.Linq; 使用System.Collections.Generic; 使用System.Windows.Forms; 使用System.Collections.Concurrent; 使用System.Threading.Tasks

我正在努力改进一个1bpp的图像搜索功能,我把它放在一起

我参考了鲍伯·鲍威尔的优秀页面,以及这里的一些答案,我能够整理出这个非常有效的例程

使用系统图; 使用系统、绘图、成像; 使用System.Runtime.InteropServices; 使用System.Linq; 使用System.Collections.Generic; 使用System.Windows.Forms; 使用System.Collections.Concurrent; 使用System.Threading.Tasks; 名称空间汽车人 { 公共部分类ActiveScreenMatch { 公共静态bool screenmatch矩形rect=默认值,字符串路径= { 如果rect==default&&string.IsNullOrEmptypath { 返回false; } 位图bw; 如果rect==默认值 { bw=新的位图路径; bw=bw.Clonenew矩形新点0,0,新尺寸bw.Width,bw.Height,PixelFormat.Format1BPindexed; } 其他的 { bw=GetBlackWhiteAtrect.Location,rect.Size; } ///初始化搜索图像数组。 bool[]ba1; 使用bw { BitmapData data=bw.LockBitsnew Rectangle0,0,bw.Width,bw.Height,ImageLockMode.ReadOnly,PixelFormat.Format1Bppined; ba1=新布尔值[bw.高度][]; 对于int y=0;y x { 打破 } 其他的 { 跳过黑线++; } } bool[]ba2; 使用位图SearchWindow=GetBlackWhiteAtnew Point0,0,new SizeScreen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height { BitmapData data=SearchWindow.LockBitsnew Rectangle0,0,SearchWindow.Width,SearchWindow.Height,ImageLockMode.ReadOnly,PixelFormat.Format1Bppined; ba2=新bool[SearchWindow.Height][]; 对于int y=0;y { var Base=ba1.Skipskippedblackline; 对于int i=item.Item2;i!=item.Item1;i- { 如果sublistinexba2[i].AsEnumerable,0,Base.LastOrDefault!=-1 { 如果Base.Count==1 { 循环状态。中断; 移动到 SubListIndexba2[i]。AsEnumerable,0,Base.LastOrDefault+ba1[0]。长度/2, i+ba1.GetUpperBound0/2; } 其他的 { Base=Base.TakeBase.Count-1; } } } }; return!Result.IsCompleted; } 私有静态int-GetIndexedPixelint x,int-y,位图数据 { var指数=y*data.Stride+x>>3; 变量掩码=字节0x80>>x&0x7; 字节ret=Marshal.ReadBytedata.Scan0,索引; ret&=面罩; 返回ret; } 私有静态int子列表IndexiEnumerable列表、int开始、IEnumerable子列表 { 对于int listIndex=start;listIndex 任何输入或想法都会很好。

如果您使用关键字,如果我正确理解问题,您可以反过来使用指针按索引提取字节

unsafe关键字表示不安全上下文,这是 任何涉及指针的操作

为此,必须将您的方法标记为


注意:您需要设置项目构建选项以允许不安全的代码

是的,我可以直接访问内存来读取位,完全避免Marshal.ReadByte。但我想读取字节8位,并返回其中x在该范围内的索引,为该组计算记忆字节:@BanMe我建议在问题中添加这些信息和更多信息,因为不太清楚你想要实现什么。
byte p = *((byte*)data.Scan0 + index);
unsafe int GetIndexedPixel(int x, int y, BitmapData data)