我正在优化我正在处理的程序,该程序当前使用锁定位读取字节数据,但使用 setPixel 写入像素数据。那么我如何实际修改我正在读取的像素数据呢?如果我尝试设置 pp、cp 或 np,该方法将不起作用(因为它循环并需要 pp、cp 和 np 来表示像素数据),所以我完全糊涂了。我是否需要将像素数据写入 byte[] 并对其进行操作,还是什么?
这是一个代码示例:
BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int scaledPercent = (int)(Math.Round(percentageInt * 255)) - 47;
Debug.WriteLine("percent " + scaledPercent);
unsafe
{
Debug.WriteLine("Woah there, unsafe stuff");
byte* prevLine = (byte*)data.Scan0;
byte* currLine = prevLine + data.Stride;
byte* nextLine = currLine + data.Stride;
for (int y = 1; y < img.Height - 1; y++)
{
byte* pp = prevLine + 3;
byte* cp = currLine + 3;
byte* np = nextLine + 3;
for (int x = 1; x < img.Width - 1; x++)
{
if (IsEdgeOptimized(pp, cp, np, scaledPercent))
{
//Debug.WriteLine("x " + x + "y " + y);
img2.SetPixel(x, y, Color.Black);
}
else
{
img2.SetPixel(x, y, Color.White);
}
pp += 3; cp += 3; np += 3;
}
prevLine = currLine;
currLine = nextLine;
nextLine += data.Stride;
}
}
img.UnlockBits(data);
pictureBox2.Image = img2;