C#实现图像反色的方法
本文实例讲述了C#实现图像反色的方法。分享给大家供大家参考。具体如下:
//定义图像反色函数
privatestaticBitmapPContray(Bitmapa)
{
intw=a.Width;
inth=a.Height;
BitmapdstBitmap=newBitmap(a.Width,a.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapDatasrcData=a.LockBits(newRectangle(0,0,w,h),System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapDatadstData=dstBitmap.LockBits(newRectangle(0,0,w,h),System.Drawing.Imaging.ImageLockMode.WriteOnly,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte*pIn=(byte*)srcData.Scan0.ToPointer();
byte*pOut=(byte*)dstData.Scan0.ToPointer();
byte*p;
intstride=srcData.Stride;
intr,g,b;
for(inty=0;y<h;y++)
{
for(intx=0;x<w;x++)
{
p=pIn;
r=p[2];
g=p[1];
b=p[0];
pOut[2]=(byte)(255-r);
pOut[1]=(byte)(255-g);
pOut[0]=(byte)(255-b);
pIn+=3;
pOut+=3;
}
pIn+=srcData.Stride-w*3;
pOut+=srcData.Stride-w*3;
}
a.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
returndstBitmap;
}
}
希望本文所述对大家的C#程序设计有所帮助。