在C 中读取并编写BMP文件
read and write a bmp file in c++
我正在尝试在C 中读取和编写一个BMP文件。输出文件是创建但不是打开的,它的大小为257kb,而输入文件为258kb.i首先读取并写入14个字节标头文件,40个字节imageheader文件,然后是512*512像素,这是我的代码,任何人都可以帮忙
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
ifstream iFile;
char ch;
iFile.open("lena.bmp",ios::binary);
ofstream oFile;
oFile.open("lena3.bmp",ios::binary);
//int headerImageHeader=54;
// int imageHeader=40;
int fs[54];
//int ihs[imageHeader];
int tfs[54];
int pixel[512][512];
if(iFile.is_open() && oFile.is_open())
{
for(int i=0;i<54;i++)
{
iFile.get(ch);
fs[i]=ch;
cout<<fs[i]<<" ";
char p;
p=fs[i];
oFile<<p;
}
for(int w=0;w<512;w++)
{
for(int h=0;h<512;h++)
{
iFile.get(ch);
pixel[w][h]=ch;
//cout<<pixel[w][h]<<;
char pi=pixel[w][h];
oFile<<pi;
}
}
oFile.close();
iFile.close();
}
else cout << "Unable to open file"<<endl;
return 0;
}
您的代码有多个问题。基本上,BMP文件中的像素存储并不是那么简单。它取决于BMP的类型(例如Monochrome Bitmap
,16 Color Bitmap
,256 Color Bitmap
和24-bit Bitmap
等)。
有两个选项可以读取一个位图文件并将其写入另一个位图文件。a)从源文件中读取每个字节并在目标文件中写入b)了解Bitmap
存储格式并相应地编写代码。
对于选项b)
,请访问良好的来源。
总而言之,像素阵列存储的计算(大小)为:
PixelArraySize = RowSize x ImageHeight
其中ImageHeight
是像素中图像的高度。排名计算为:
RowSize = [(BitsPerPixelxImageWidth + 31) / 32 ]x4
相关文章:
- 从 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 文件转换为数组