我正在尝试用C ++创建一个没有库的位图文件。我已经尝试过这段代码,但它不起作用。我还希望能够编辑位图图像中的像素。根据我的代码,这应该创建一个没有图像数据的位图文件,但至少应该打开位图图像吗?它说它已经损坏了。
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
using namespace std;
#include <Windows.h>
int main() {
FILE *fileBuffer = NULL;
fileBuffer = fopen("test.bmp", "w");
BITMAPFILEHEADER *fileHeader = new BITMAPFILEHEADER;
BITMAPINFOHEADER *infoHeader = new BITMAPINFOHEADER;
int imageWidth = 0;
int imageHeight = 0;
fileHeader->bfType = 'BM';
fileHeader->bfSize = sizeof(BITMAPFILEHEADER);
fileHeader->bfReserved1 = 0;
fileHeader->bfReserved2 = 0;
fileHeader->bfOffBits = 54;
fwrite(fileHeader, 1, 14, fileBuffer);
infoHeader->biSize = sizeof(BITMAPINFOHEADER);
infoHeader->biWidth = imageWidth;
infoHeader->biHeight = imageHeight;
infoHeader->biPlanes = 1;
infoHeader->biBitCount = 24;
infoHeader->biCompression = BI_RGB;
infoHeader->biSizeImage = sizeof(RGBTRIPLE) * imageWidth * imageHeight;
infoHeader->biXPelsPerMeter = 0;
infoHeader->biYPelsPerMeter = 0;
infoHeader->biClrImportant = 0;
infoHeader->biClrUsed = 0;
fwrite(infoHeader, 1, 40, fileBuffer);
fclose(fileBuffer);
delete fileHeader;
delete infoHeader;
return 0;
}