代码如下:
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#pragma pack(2)//必须得写,否则sizeof得不到正确的结果
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
typedef struct {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
void saveBitmapData(int w, int h, unsigned char* pData, int nDatasize)
{
// Define BMP Size
const int height = w;
const int width = h;
const int size = nDatasize;
double x, y;
int index;
// Part.1 Create Bitmap File Header
BITMAPFILEHEADER fileHeader;
fileHeader.bfType = 0x4D42;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// Part.2 Create Bitmap Info Header
BITMAPINFOHEADER bitmapHeader = { 0 };
bitmapHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapHeader.biHeight = -height;
bitmapHeader.biWidth = width;
bitmapHeader.biPlanes = 1;
bitmapHeader.biBitCount = 32;
bitmapHeader.biSizeImage = size;
bitmapHeader.biCompression = 0; //BI_RGB
// Write to file
FILE* output = fopen("output.bmp", "wb");
if (output == NULL)
{
printf("Cannot open file!\n");
}
else
{
fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, output);
fwrite(&bitmapHeader, sizeof(BITMAPINFOHEADER), 1, output);
fwrite(pData, size, 1, output);
fclose(output);
}
}
void saveBitmap()
{
// Define BMP Size
const int height = 600;
const int width = 800;
const int size = height * width * 3;
double x, y;
int index;
// Part.1 Create Bitmap File Header
BITMAPFILEHEADER fileHeader;
fileHeader.bfType = 0x4D42;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// Part.2 Create Bitmap Info Header
BITMAPINFOHEADER bitmapHeader = { 0 };
bitmapHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapHeader.biHeight = height;
bitmapHeader.biWidth = width;
bitmapHeader.biPlanes = 3;
bitmapHeader.biBitCount = 24;
bitmapHeader.biSizeImage = size;
bitmapHeader.biCompression = 0; //BI_RGB
// Part.3 Create Data
BYTE* bits = (BYTE*)malloc(size);
// Clear
memset(bits, 0xFF, size);
// Sin Graph
for (x = 0; x < 800; x += 0.5)
{
y = sin(x / 100.0) * 200 + 300;
index = (int)y * 800 * 3 + (int)x * 3;
bits[index + 0] = 255; // Blue
bits[index + 1] = 0; // Green
bits[index + 2] = 0; // Red
}
// Write to file
FILE* output = fopen("output.bmp", "wb");
if (output == NULL)
{
printf("Cannot open file!\n");
}
else
{
fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, output);
fwrite(&bitmapHeader, sizeof(BITMAPINFOHEADER), 1, output);
fwrite(bits, size, 1, output);
fclose(output);
}
}
int main(int argc, const char** argv)
{
saveBitmap();
return 0;
}
生成consoleApp输出图像如下: