C + + reads and stores a BMP image

Source: Internet
Author: User


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

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.