没有语法错误,仅供参考。
//private bool ContourDIB(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
private bool ContourDIB(ref byte[] lpDIBBits, int lWidth, int lHeight)
{
try
{
指向原图象的指针
// LPSTR lpSrc;
指向缓存图象的指针
// LPSTR lpDst;
指向缓存DIB图象的指针
// LPSTR lpNewDIBBits;
// HLOCAL hNewDIBBits;
循环变量
// long i;
// long j;
byte n, e, s, w, ne, se, nw, sw;
像素值
byte pixel;
暂时分配内存,以保存新图象
// hNewDIBBits=LocalAlloc(LHND,lWidth*lHeight);
//byte[] hNewDIBBits = new byte[lWidth * lHeight];
//byte[] lpSrc = lpDIBBits;
byte[] lpDst = new byte[lWidth * lHeight];
// if(hNewDIBBits==NULL)
// {
// return FALSE;
// }
// lpNewDIBBits=(char *)LocalLock(hNewDIBBits);
// //初始化新分配的内存,设定初始值为255
// lpDst=(char *)lpNewDIBBits;
// memset(lpDst,(byte)255,lWidth*lHeight);
for (int j = 1; j < lHeight - 1; j++)
{
for (int i = 1; i < lWidth - 1; i++)
{
//指向原图象倒数第J行,第I个像素的指针
//lpSrc = lpDIBBits[lWidth * j + i];
//指向目标图象倒数第J行,第I个像素的指针
//lpDst = lpNewDIBBits[lWidth * j + i];
//取得当前指针处的像素值,注意要转换为unsigned char型
//pixel = *lpSrc;
pixel = lpDIBBits[lWidth * j + i];
if (pixel == 0)
{
//*lpDst = 0;
lpDst[lWidth * j + i] = 0;
//nw = *(lpSrc + lWidth - 1);
nw = lpDIBBits[lWidth * j + i + lWidth - 1];
//n = *(lpSrc + lWidth);
n = lpDIBBits[lWidth * j + i + lWidth];
//ne = *(lpSrc + lWidth + 1);
ne = lpDIBBits[lWidth * j + i + lWidth + 1];
//w = *(lpSrc - 1);
w = lpDIBBits[lWidth * j + i - 1];
//e = *(lpSrc + 1);
e = lpDIBBits[lWidth * j + i + 1];
//sw = *(lpSrc + lWidth - 1);
sw = lpDIBBits[lWidth * j + i + lWidth - 1];
//s = *(lpSrc + lWidth);
s = lpDIBBits[lWidth * j + i + lWidth];
//se = *(lpSrc + lWidth + 1);
se = lpDIBBits[lWidth * j + i + lWidth + 1];
//如果相邻的8个点都是黑点
if (nw + n + ne + w + e + sw + s + se == 0)
//*lpDst = 255;
lpDst[lWidth * j + i] = 255;
}
}
}
//复制运算后的图象
//memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);
Buffer.BlockCopy(lpDst, 0, lpDIBBits, 0, lWidth * lHeight);
//释放内存
//LocalUnlock(hNewDIBBits);
//LocalFree(hNewDIBBits);
//返回
return true;
}
catch { }
return false;
}