C ++ reads and stores a BMP Image

Source: Internet
Author: User

C ++ reads and stores a BMP Image

 

 

 

C ++ source code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; void main () {char fileName [30]; // defines the name of the opened image char * buf; // defines the file to read the buffer char * p; int r, g, b, pix; HWND wnd; // window handle HDC dc; // drawing device environment handle FILE * fp; // define FILE pointer FILE * fpw; // define the storage file pointer DWORD w, h; // define the length and width of the read image DWORD bitCorlorUsed; // define the DWORD bitSize; // define the image size BITMAPFILEHEADER bf; // Image File Header BITMAPINFOHEADER bi; // image file header information cout <enter the name of the file to be opened:; cin> fileName; if (fp = fopen (fileName, rb) = NULL) {cout <file not found !; Exit (0);} fread (& bf, sizeof (BITMAPFILEHEADER), 1, fp); // read the BMP file header file fread (& bi, sizeof (BITMAPINFOHEADER), 1, fp); // read the header file information of the BMP file w = bi. biWidth; // obtain the image width h = bi. biHeight; // obtain the image's high bitSize = bi. biSizeImage; // obtain the image's sizebuf = (char *) malloc (w * h * 3); // allocate the buffer size fseek (fp, long (sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER), 0); // locate the starting position of the pixel fread (buf, 1, w * h * 3, fp ); // start to read data wnd = GetForegroundWindow (); // obtain the window handle dc = GetDC (wnd); // obtain the drawing device int x = 40; int y = 40; p = buf; for (int j = 0; j
       
        

 

 

Input File Name:

 

Show image:

Stored files:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.