能生成BMP

能生成BMP| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |

| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 16937996| 能生成BMP| 2022-11-30T11:54:00| | BlogPost|

能生成BMP

http://bbs.bccn.net/thread-336164-1-1.html    为何该程序生成的BMP图像打不开??
 
程序代码:
 
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#include <pshpack2.h>
#include <poppack.h>
 
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
 
 
/*定义位图文件头*/
typedef struct tag_BIT_MAP_FILE_HEADER
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BIT_MAP_FILE_HEADER;
 
/*定义信息头*/
typedef struct tagBIT_MAP_INFO_HEADER
{
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BIT_MAP_INFO_HEADER;
 
/*调色板*/
typedef struct tagRGB
{
BYTE blue;
BYTE green;
BYTE red;
} RGB;
 
 
 
int main()
{
RGB *bmp_data = NULL;
FILE *bmp_file;
long i;
long width = 80;
long height = 80;
long date_size = width*height;
BIT_MAP_FILE_HEADER bmp_map_file;
BIT_MAP_INFO_HEADER bmp_map_info;
 
if((bmp_file=fopen("cubs.bmp", "wb+")) == NULL)
{
printf("Error!\n");
system("pause");
exit(0);
}
/*写入图位文件头*/
bmp_map_file.bfType = 0x4d42;
bmp_map_file.bfSize = 14 + 40 + width * height * 3;
bmp_map_file.bfReserved1 = 0;
bmp_map_file.bfReserved2 = 0;
bmp_map_file.bfOffBits = 0x36;
fwrite(&bmp_map_file, sizeof(BIT_MAP_FILE_HEADER), 1, bmp_file);
 
/*写入信息头*/
bmp_map_info.biSize = 40;
bmp_map_info.biPlanes = 1;
bmp_map_info.biHeight = height;
bmp_map_info.biWidth = width;
bmp_map_info.biBitCount = 24;
bmp_map_info.biClrImportant = 0;
bmp_map_info.biClrUsed = 0;
bmp_map_info.biSizeImage = width*height*3;
bmp_map_info.biCompression = 0;
bmp_map_info.biXPelsPerMeter = 0;
bmp_map_info.biYPelsPerMeter = 0;
fwrite(&bmp_map_info, sizeof(BIT_MAP_INFO_HEADER), 1, bmp_file);
 
/*给图像分配内存*/
if((bmp_data=(RGB*)malloc(width * height * 3)) == NULL)
{
printf("申请内存失败!\n");
exit(0);
}
/*RGB赋值并写入文件中*/
for(i= 0; i<width*height; i++)
{
bmp_data[i].green=255;
bmp_data[i].red=0;
bmp_data[i].blue=0;
}
fwrite(bmp_data, sizeof(RGB), date_size, bmp_file);
 
printf("BMP图像生成成功!\n\n\n");
fclose(bmp_file);
free(bmp_data);
bmp_data = NULL;
return 0;
}
 
 
//============================================================================================
 
#pragma pack(push)
#pragma pack(1)
 
/*定义位图文件头*/
typedef struct tag_BIT_MAP_FILE_HEADER
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BIT_MAP_FILE_HEADER;
 
/*定义信息头*/
typedef struct tagBIT_MAP_INFO_HEADER
{
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BIT_MAP_INFO_HEADER;
 
/*调色板*/
typedef struct tagRGB
{
BYTE blue;
BYTE green;
BYTE red;
} RGB;
 
#pragma pack(pop)
 
如果你用的是VC,这样改一下再编译就行了 
| 648658| | 2022-11-30T11:54:00| false| | 2022-11-30T11:53:31.437| true| 能生成BMP http://bbs.bccn.net/thread-336164-1-1.html 为何该程序生成的BMP图像打不开?? 程序代码: #include <stdio.h> #include <time.h> #include <stdlib.h> #include <malloc.h| Anonymous|
posted @   RalphLauren  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示