C語言讀BMP圖片

來源:互聯網
上載者:User

#include<windows.h> //包含相關bmp格式的定義結構<br />#include<stdio.h><br />#include<stdlib.h></p><p>int bmpWidth; //bmp圖片寬度(像素)<br />int bmpHeight; //bmp圖片高度(像素)<br />RGBQUAD* pColorTable; //調色盤數組(真彩色時不用)<br />int biBitCount; //每像素所佔的位元組</p><p>/*************************************************************<br />* 名稱:Read_BMP<br />* 功能:讀bmp映像,返回讀出的16進位數組<br />* 入口參數:bmpfilename:bmp圖片名<br />* 出口參數:無<br />**************************************************************/<br />unsigned char* Read_BMP(const char* bmpfilename)<br />{<br />unsigned char* pBmpBuf; //儲存緩衝區<br />FILE* fp = fopen(bmpfilename,"rb"); //二進位制度開啟<br />if(fp == NULL)<br />{<br />printf("Can not open this file");<br />return 0;<br />}<br />fseek(fp,sizeof(BITMAPFILEHEADER),0); //跳過BMP頭<br />BITMAPINFOHEADER infoheader;<br />fread(&infoheader,sizeof(BITMAPINFOHEADER),1,fp); //讀bmp圖片資訊<br />bmpWidth = infoheader.biWidth;<br />bmpHeight = infoheader.biHeight;<br />biBitCount = infoheader.biBitCount;<br />int line_Byte = (bmpWidth*biBitCount/8+3)/4*4; //每行所佔位元組數<br />if(biBitCount == 8)<br />{<br />pColorTable = new RGBQUAD[256];<br />fread(pColorTable,sizeof(RGBQUAD),256,fp);<br />}<br /> pBmpBuf = new unsigned char [line_Byte*bmpHeight]; //開闢儲存區<br />fread(pBmpBuf,1,line_Byte*bmpHeight,fp);<br />fclose(fp); //關閉檔案<br />return pBmpBuf;<br />}</p><p>/*************************************************************<br />* 名稱:main<br />* 功能:測試函數<br />* 入口參數:無<br />* 出口參數:無<br />**************************************************************/<br />int main()<br />{<br />unsigned char* Buffer=Read_BMP("1.bmp"); //讀bmp圖片<br />FILE* fp = fopen("test.txt","wb");<br />fwrite(Buffer,1,(bmpWidth*biBitCount/8+3)/4*4*bmpHeight,fp); //將讀出的圖片資料寫入test.txt<br />return 0;<br />}

這裡有一個問題 ,存入文字檔的16進位資料是亂碼的,需要用16進位編輯工具讀出;而且預設bmp圖片讀圖書序是從左下角開始的,這樣在在TFT上顯示的時候,需要注意先從左下角開始掃描,或者先將資料轉換成左上方開始的順序。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.