我的代码在尝试创建BMP文件时失败
My code fails while attempting to create a BMP file
我正在尝试创建一个.bmp文件,但无法识别该文件。我做错了什么?
#define ImageWidgh 1920
#define ImageHeight 1080
#define fileSize ImageWidgh*ImageHeight*3+54
struct BMPH
{
short Signature;
long int FileSize,reserved,DataOffest;
}BMPH;
struct BMPIH
{
long int Size,Width,Height;
short Planes,BitCount;
long int Compression,ImageSize,XpixelsPerM,YpixelsPerM,ColorsUsed,ColorImportant;
}BMPIH;
struct BMPCT
{
unsigned char Red,Green,Blue;
}BMPCT;
BMPH *getBMPH()
{
BMPH New;
New.Signature='BM';
New.FileSize=fileSize;
New.reserved=0;
New.DataOffest=54;
return &New;
}
BMPIH *getBMPIH()
{
BMPIH New;
New.Size=40;
New.Width=ImageWidgh;
New.Height=ImageHeight;
New.Planes=1;
New.BitCount=24;
New.Compression=0;
New.ImageSize=0;
New.XpixelsPerM=0;
New.YpixelsPerM=0;
New.ColorsUsed=0;
New.ColorImportant=0;
return &New;
}
BMPCT Pixels [ImageWidgh][ImageHeight];
void writeFile()
{
FILE *file;
file=fopen("D://test.bmp","wb");
fwrite(getBMPH() ,sizeof(BMPH) ,1,file);
fwrite(getBMPIH(),sizeof(BMPIH),1,file);
fwrite(&Pixels ,ImageWidgh*ImageHeight*3,1,file);
fclose(file);
}
一个典型的错误:返回一个指向局部变量的指针,该指针在离开函数后立即被销毁。指针最终指向胡言乱语或更糟的地方。
让函数返回完整的结构而不是指针,或者将指针传递到函数中而不是在函数中创建变量。
这对我来说不太合适:
New.Signature='BM';
你当然想要:
New.Signature = *(short*)"BM";
相关文章:
- 从 bmp 文件数据创建 MFC CBitmap
- 将 bmp 文件加载到 TSpeedButton 中
- 在 BMP 文件中查找隐藏的消息
- 如何在C++的四叉树中存储 bmp 文件?
- C++:读取.BMP文件时出现问题;文件结束时间早于预期
- 无法加载.bmp文件win32
- 读取BMP文件会归还意外数据
- 在C++中读取bmp文件的宽度和高度
- 在C 中读取并编写BMP文件
- 在BMP文件中保存屏幕截图的设备上下文
- 读取 BMP 文件C++(读取 BMP 标头时出现问题)
- BMP 文件,访问数据
- BMP文件行填充问题
- 我的代码在尝试创建BMP文件时失败
- 在C++、Ubuntu中使用BMP文件
- 当尝试读取和写入bmp文件c++时中止
- 在C++中获取指向bmp文件中dib的指针
- 在 cpp 中从 mysql blob 解析 BMP 文件
- 如何将这个新字符**写入.bmp文件
- 在 C/C++ 中将 64 位 bmp 文件转换为数组