This article describes the image grayscale method of C# digital image processing. Share it for your reference. The details are as follows:
//Define image grayscale functionprivate static Bitmap PGray(Bitmap src) { int w = ; int h = ; //Build a template image with the same size as the original image Bitmap dstBitmap = new Bitmap(, , .Format24bppRgb); //Storing the original image into memory srcData = (new Rectangle(0, 0, w, h), , .Format24bppRgb); dstData = (new Rectangle(0, 0, w, h), , .Format24bppRgb); unsafe { byte* pIn = (byte*)srcData.(); byte* pOut = (byte*)dstData.(); byte* p; int stride = ; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; r = p[2]; g = p[1]; b = p[0]; //Calling the image grayscale formula pOut[0] = pOut[1] = pOut[2] = (byte)(b * 0.114 + g * 0.587 + r * 0.299); pIn += 3; pOut += 3; } pIn += - w * 3; pOut += - w * 3; } (srcData); (dstData); return dstBitmap; } }
I hope this article will be helpful to everyone's C# programming.