C + + reads and stores a BMP image
Reprint please indicate the source
Previously wrote an article introducing BMP format, this is the basis of C + + processing Digital image, only know the BMP file in the computer storage structure, can talk about after the image processing. Here is the connection address: http://blog.csdn.net/carvin_zh/article/details/45875277
C + + Source:
#include <iostream> #include <Windows.h> #include <malloc.h> #include <stdlib.h> #include < Stdio.h> #include <string.h>using namespace std;void main () {char filename[30]; Define open image name Char *buf; Defines the file read buffer char *p;int R,g,b,pix; HWND Wnd; Window handle HDC DC; Drawing device environment handle file *FP; Defines a file pointer to document *FPW; Defines the save file pointer DWORD w,h; Defines the long and wide DWORD bitcorlorused of the read image; Defines the DWORD bitsize; Defines the size of the image bitmapfileheader BF; Image file Header Bitmapinfoheader bi; Image file header information cout<< "Please enter the name of the file to open:"; Cin>>filename;if ((Fp=fopen (FileName, "RB") ==null) {cout<< "File not found!) "; Exit (0);} Fread (&bf,sizeof (Bitmapfileheader), 1,FP);//Read BMP file Header file Fread (&bi,sizeof (Bitmapinfoheader),1,FP);//Read BMP file header file information w=bi.biwidth; Obtain the wide h=bi.biheight of the image; Get the high bitsize=bi.bisizeimage of the image; Gets the image of Sizebuf= (char*) malloc (w*h*3); Allocate buffer size fseek (fp,long (sizeof (Bitmapfileheader) +sizeof (bitmapinfoheader)), 0);//position to pixel start position fread (BUF,1,W*H*3,FP); Start reading data Wnd=getforegroundwindow (); Gets the window handle DC=GETDC (WND); Get drawing device int x=40;int y=40;p=buf;for (int j=0;j
Enter the file name:
Show Picture:
Files Stored:
C + + reads and stores a BMP image