//create grayscal bitmap from one band public Bitmap createGrayscaleImagefromOneBand(byte[] byIn, int imgWidth, int imgHeight, int Stride, int BytesPerPixel) { Bitmap bmp = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb); byte[] byOut = new byte[Stride * imgHeight]; Int32 ind = 0; for (int i = 0; i < imgHeight; i++) { for (int j = 0; j < imgWidth; j++) { byte b = byIn[i * imgWidth + j]; ind = i * Stride + j * BytesPerPixel; for (int k = 0; k < 3; k++) { byOut[ind] = b; ind++; } } } imageByteArray imba = new imageByteArray(); bmp = imba.ByteArrayToBitmap(byOut, imgWidth, imgHeight); return(bmp); }
// get one band from the given bitmap to a byte array public byte[] getOneBandtoByteArray(Bitmap img, int band) { imageByteArray imba = new imageByteArray(img); byte[] byIn = imba.BitmapToByteArray(img); int res = (img.Width) % 4; Int32 stride = imba.Stride; Int32 length = (Int32)(stride * img.Height); byte[] byOut = new byte[length]; Int32 ind = 0; Int32 indOut = 0; for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) { ind = i * stride + j * imba.BytesPerPixel; int col = byIn[ind + 2 - band]; byOut[indOut] = (byte)col; indOut += 1; } } return(byOut); }