Help. Create a bmp file in C++
-
Hi all: I tried to sort out creating a bmp file and save an image in C++. It really annoyed me. Following is my test code, but it doesn't work. Could somebody give me an idea how to create a bmp file and store an image please???? Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void draw(){ int x[100]; int n = 0; for(int i = 0; i < 10; i ++){ for (int j = 0; j < 10; j ++){ x[i*10+j] = n++; if (n >= 255) n = 0; } } HBITMAP map = CreateBitmap(10, 10, 1, 8, x); if (map == NULL) cout << "cao ni ma\n"; HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header BITMAPINFOHEADER bih; // bitmap info-header LPCTSTR filename = "output.bmp"; DWORD dwTmp; hdr.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(x); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = 100; bih.biHeight = 100; bih.biPlanes = 1; bih.biBitCount = 8; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, NULL); WriteFile(hf, &bih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL); WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL); } Asura
-
Hi all: I tried to sort out creating a bmp file and save an image in C++. It really annoyed me. Following is my test code, but it doesn't work. Could somebody give me an idea how to create a bmp file and store an image please???? Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void draw(){ int x[100]; int n = 0; for(int i = 0; i < 10; i ++){ for (int j = 0; j < 10; j ++){ x[i*10+j] = n++; if (n >= 255) n = 0; } } HBITMAP map = CreateBitmap(10, 10, 1, 8, x); if (map == NULL) cout << "cao ni ma\n"; HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header BITMAPINFOHEADER bih; // bitmap info-header LPCTSTR filename = "output.bmp"; DWORD dwTmp; hdr.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(x); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = 100; bih.biHeight = 100; bih.biPlanes = 1; bih.biBitCount = 8; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, NULL); WriteFile(hf, &bih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL); WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL); } Asura
Hello Luo ming, Take a look at "programing windows", there is intensive discussion about bitmap stuff. Good luck!
-
Hi all: I tried to sort out creating a bmp file and save an image in C++. It really annoyed me. Following is my test code, but it doesn't work. Could somebody give me an idea how to create a bmp file and store an image please???? Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! void draw(){ int x[100]; int n = 0; for(int i = 0; i < 10; i ++){ for (int j = 0; j < 10; j ++){ x[i*10+j] = n++; if (n >= 255) n = 0; } } HBITMAP map = CreateBitmap(10, 10, 1, 8, x); if (map == NULL) cout << "cao ni ma\n"; HANDLE hf; // file handle BITMAPFILEHEADER hdr; // bitmap file-header BITMAPINFOHEADER bih; // bitmap info-header LPCTSTR filename = "output.bmp"; DWORD dwTmp; hdr.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(x); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfType = 0x4d42; hdr.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = 100; bih.biHeight = 100; bih.biPlanes = 1; bih.biBitCount = 8; bih.biCompression = BI_RGB; bih.biSizeImage = 0; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, NULL); WriteFile(hf, &bih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwTmp, NULL); WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL); } Asura
Ming Luo wrote:
WriteFile(hf, (LPSTR) x, 100, (LPDWORD) &dwTmp, NULL);
for starters, for a 100x100 RGB image, you need to write 300 bytes worth of pixel data per each pixel row. looks like you're only writing 100 bytes total. Cleek | Image Toolkits | Thumbnail maker