BYTE* buf = new BYTE[ 128 * 3 * SizeValue ]; int c = 0; for ( int i = 0; i < SizeValue; i++ ) { for ( int j = 0; j < 128; j++ ) { unsigned char val = pmatrix[ i ][ j ] == 0 ? 0xFF : 0x00; buf[ c + 0 ] = (BYTE) val; buf[ c + 1 ] = (BYTE) val; buf[ c + 2 ] = (BYTE) val; c += 3; } } SaveBitmapToFile( (BYTE*) buf, 128, SizeValue, 24, "C:\\MyFolder\\image_created.bmp" ); delete [] buf; Saving the byte data as a bitmap file Writing the array data as a bitmap file is accomplished by the SaveBitmapToFile module. This module essentially: i. initializes a BITMAPINFOHEADER data structure with bitmap parameters (header size, padding, height, width, etc) ii. initializes a BITMAPFILEHEADER structure in the appropriate way iii. Creates a file handler and writes the file, bitmap info and pixel data into it to create the new bitmap file representation: // // Save the bitmap to a bmp file // void CSimulationrun::SaveBitmapToFile( BYTE* pBitmapBits, LONG lWidth, LONG lHeight, WORD wBitsPerPixel, LPCTSTR lpszFileName ) { // Some basic bitmap parameters unsigned long headers_size = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ); unsigned long padding_size = ( 4 - ( ( lWidth * 3 ) % 4 ) ) % 4; unsigned long pixel_data_size = lHeight * ( ( lWidth * 3 ) + padding_size ); BITMAPINFOHEADER bmpInfoHeader = {0}; // Set the size bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); // Bit count bmpInfoHeader.biBitCount = wBitsPerPixel; // Use all colors bmpInfoHeader.biClrImportant = 0; // Use as many colors according to bits per pixel bmpInfoHeader.biClrUsed = 0; // Store as un Compressed bmpInfoHeader.biCompression = BI_RGB; // Set the height in pixels bmpInfoHeader.biHeight = lHeight; // Width of the Image in pixels bmpInfoHeader.biWidth = lWidth; // Default number of planes bmpInfoHeader.biPlanes = 1; // Calculate the image size in bytes bmpInfoHeader.biSizeImage = pixel_data_size; BITMAPFILEHEADER bfh = {0}; // This value should be values of BM letters i.e 0x4D42 // 0x4D = M 0¡Á42 = B storing in reverse order to match with endian bfh.bfType=0x4D42; /* or bfh.bfType = ¡®B¡¯+(¡®M¡¯ << 8); // <<8 used to shift ¡®M¡¯ to end */ // Offset to the RGBQUAD bfh.bfOffBits = headers_size; // Total size of image including size of headers bfh.bfSize = headers_size + pixel_data_size; // Create the file in disk to write HANDLE hFile = CreateFile( lpszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); // Return if error opening file if( !hFile ) return; DWORD dwWritten = 0; // Write the File header WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL ); // Write the bitmap info header WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL ); // Write the RGB Data WriteFile( hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwWritten, NULL ); // Close the file handle CloseHandle( hFile ); }