C language Read BMP picture

Source: Internet
Author: User
Tags bmp image fread

#include <windows.h>//definition structure containing the relevant BMP format#include <stdio.h>#include<stdlib.h>intBmpwidth;//bmp picture Width (pixels)intBmpheight;//bmp picture Height (pixels)rgbquad* pcolortable;//palette Array (true Color not required)intbiBitCount;//bytes per pixel/************************************************************** Name: read_bmp* function: Read BMP image, return the read out of the 16 binary array * Entry parameter: Bmpfilename:bmp picture name * Export parameter: no **************************************************************/unsignedChar* Read_bmp (Const Char*bmpfilename) {unsignedChar* PBMPBUF;//Storage Buffersfile* fp = fopen (Bmpfilename,"RB");//Binary system Open    if(fp = =NULL) {printf ("Can not open the This file"); return 0; } fseek (FP,sizeof(Bitmapfileheader),0);//Skip BMP HeadBitmapinfoheader Infoheader; Fread (&infoheader,sizeof(Bitmapinfoheader),1, FP);//read BMP picture informationBmpwidth =Infoheader.biwidth; Bmpheight=Infoheader.biheight; biBitCount=Infoheader.bibitcount; intLine_byte = (bmpwidth*bibitcount/8+3)/4*4;//the number of bytes per row    if(biBitCount = =8) {pcolortable=Newrgbquad[ the]; Fread (pcolortable,sizeof(Rgbquad), the, FP); } pbmpbuf=NewUnsignedChar[Line_byte*bmpheight];//Open Storage AreaFread (Pbmpbuf,1, line_byte*BMPHEIGHT,FP);  Fclose (FP); //Close File    returnpbmpbuf;}/************************************************************** Name: main* function: Test function * Entry parameter: None * Export parameter: no *************** ***********************************************/    intMain () {unsignedChar* Buffer=read_bmp ("1.bmp");//Read BMP picturesfile* fp = fopen ("Test.txt","WB"); Fwrite (Buffer,1, (bmpwidth*bibitcount/8+3)/4*4*BMPHEIGHT,FP);//writes the read-out picture data to Test.txt    return 0;}

The 16 binary data that is stored in the text file is garbled and needs to be read with the 16 binary editing tool, and the default BMP picture reads the book order from the bottom left corner, so when the display on the TFT, you need to pay attention to start scanning from the lower left corner, or first convert the data to the upper left corner to start the order.

C language Read BMP picture

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.