C++实现读取BMP文件

头文件

#ifndef __FUNCTIONS_H_
#define __FUNCTIONS_H_
#include "Windows.h" 
using namespace std;
class IMAGE_PROCESS{
	private:
		WORD   	bfType; 
  		DWORD	bfSize; 
  		WORD	bfReserved1;
  		WORD	bfReserved2;
  		DWORD	bfOffBits;

		DWORD   biSize;   
		LONG	biWidth;  	
		LONG	biHeight;  
		WORD   	biPlanes;  
		WORD   	biBitCount;
		DWORD   biCompression;   
		DWORD   biSizeImage;  
		LONG	biXPelsPerMeter;  	
		LONG	biYPelsPerMeter;  
		DWORD   biClrUsed;
		DWORD   biClrImportant;
	
		BYTE  	rgbBlue;   
  		BYTE  	rgbGreen;	
  		BYTE  	rgbRed;		
  		BYTE  	rgbReserved;	
	
		unsigned char 	 *pBmpBuf;
		int lineByte;
		
	public:
		int read_from_file(void);
		const char* get_path(void);
		int show_information(void);
		int write_to_file(void);
};

#endif

获取并输出图片路径

const char* IMAGE_PROCESS::get_path(void)
{
	string path;
	char ppp;
	while (1) 
	{
		cout<<"Path:";
		cin>>path;
		size_t str = path.find_last_of("\\");
		cout<<"The file path is '"<>ppp;
		if(ppp=='y') 
		{
			return path.c_str();
			break; 
		}
		else cout<<"Sorry, please try again!"<

读取BMP文件

int IMAGE_PROCESS::read_from_file(void)
{   
	BITMAPFILEHEADER head_pre;
	BITMAPINFOHEADER head; 
	RGBQUAD			 *rgb;
	const char* FilePath = get_path();
	FILE *fp = fopen(FilePath,"rb");
	if(fp==0){
		cout<<"Can't find the photo"<

打印图片信息

int IMAGE_PROCESS::show_information(void)
{	
	//tagBITMAPFILEHEADER
	if(read_from_file()) return 1; 
	cout<<"bfType = "<#include 
#include "functions.h"
using namespace std;
int main(int argc, char** argv) {
	IMAGE_PROCESS Image;
	cout<<"Open an image and output the information."<

 

你可能感兴趣的:(C++)