bmp圖像c語言,bmp是圖形圖像文件嗎

本文目錄一覽:

怎麼樣在c語言中顯示bmp圖片,我要完整正確的程序,急!

lz  你好

c語言要顯示bmp位圖需要使用win32的api , 具體如下:

BOOL BitBlt(

  HDC hdcDest, // 位圖顯示目標設備環境中

  int nXDest,  // 位圖顯示在客戶區的x坐標

  int nYDest,  // 位圖顯示在客戶區的y坐標

  int nWidth,  // 位圖顯示的寬度

  int nHeight, // 位圖顯示的長度

  HDC hdcSrc,  // 源設備環境(包含需要顯示的bmp位圖)

  int nXSrc,   // 在當前位圖中顯示的開始x位置

  int nYSrc,   // 在當前位圖中顯示的開始y位置

  DWORD dwRop  // 映射模式

);

以下是源代碼:

//顯示bmp位圖

#includewindows.h

#include”resource.h”

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void DrawBrick(); 

int WINAPI WinMain(HINSTANCE hInstance,

   HINSTANCE hPrevInstance,

                   PSTR szCmdLine,

   int iCmdShow)

{

static TCHAR szAppName[] = TEXT(“Bmp”);

HWND hwnd;

MSG msg;

WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;

wndclass.lpfnWndProc = WndProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = szAppName;

if(!RegisterClass(wndclass))

{

MessageBox(NULL, TEXT(“This program requires Windows NT!”),

szAppName, MB_ICONERROR);

return 0;

}

hwnd = CreateWindow(szAppName,

TEXT(“Bmp Demo”),

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

754,

566,

NULL,

NULL,

hInstance,

NULL);

ShowWindow(hwnd, iCmdShow);

UpdateWindow(hwnd);

while(GetMessage(msg, NULL, 0, 0))

{

TranslateMessage(msg);

DispatchMessage(msg);

}

return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

static HBITMAP hBitmap; //位圖句柄  標示位圖

static int cxBitmap, cyBitmap; //位圖的長寬

BITMAP bitmap;

HDC hdc, hdcMem;

HINSTANCE hInstance;

PAINTSTRUCT ps;

switch(message)

{

case WM_CREATE:

hInstance = ((LPCREATESTRUCT)lParam)-hInstance; //獲取窗口的實例句柄

hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1)); //將位圖加載到內存中

GetObject(hBitmap, sizeof(BITMAP), bitmap);

cxBitmap = bitmap.bmWidth;//獲取位圖的長

cyBitmap = bitmap.bmHeight;//獲取位圖的寬

return 0 ;

case WM_PAINT:

hdc = BeginPaint(hwnd, ps);

hdcMem = CreateCompatibleDC(hdc);//創建一個兼容於hdc設備環境描述表的hdcMem  主要是用於在內存中截圖

SelectObject(hdcMem, hBitmap);   //將位圖選到hdcMem中

BitBlt(hdc, -1, -1, cxBitmap, cyBitmap, hdcMem, 0, 0, SRCCOPY);//繪製bmp位圖

DeleteDC(hdcMem);

EndPaint(hwnd, ps);

return 0;

case WM_DESTROY:

DeleteObject(hBitmap);

PostQuitMessage(0);

return 0;

}

return DefWindowProc(hwnd, message, wParam, lParam);

}

程序運行效果:

希望能幫助你哈

ps:

附件是整個工程 , 用vs2008創建的項目 , 裡面包含相應資源

c語言,怎樣讀取一個BMP圖片?

#ifndef IMAGE_H

#define IMAGE_H

void image_info(FILE* file);

void image_save(FILE *file);

void image_gray();

void image_binarization();

void image_opposite();

void image_channel(); //抽取RGB通道

void image_bright();//改變圖像亮度

typedef struct BMP

{

//14字節

unsigned short bfType; //文件標識 2字節 必須為BM

unsigned int bfSize; //文件大小 4字節

unsigned short bfReserved1; //保留,每字節以”00″填寫 2字節

unsigned short bfReserved2; //同上 2字節

unsigned int bfOffBits; //記錄圖像數據區的起始位置(圖象數據相對於文件頭字節的偏移量)。 4字節

//40字節

unsigned int biSize; //表示本結構的大小 4字節

int biWidth; //位圖的寬度 4字節

int biHeight; //位圖的高度 4字節

unsigned short biPlanes; //永遠為1 , 2字節

unsigned short biBitCount; //位圖的位數 分為1 4 8 16 24 32 2字節

unsigned int biCompression; //壓縮說明 4字節

unsigned int biSizeImage; //表示位圖數據區域的大小以字節為單位 4字節

int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字節

int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字節

unsigned int biClrUsed; //位圖使用的顏色索引數 4字節

unsigned int biClrImportant; //對圖象顯示有重要影響的顏色索引的數目 4字節

} BMP;

int line_byte;

unsigned char *imagedata;

extern BMP bmp;

extern int line_byte;

extern unsigned char *imagedata;

#endif

//image_rw.c文件

#includestdio.h

#includestdlib.h

#include”image.h”

void image_info(FILE *file)

{

int times=3; //輸入文件名次數。

char bmp_name[10]; //文件名

printf(“\nplease enter a file name for reading:”);

do

{

if (times3)

{

printf(“\nplease enter a file name for reading again:”);

}

fflush(stdin);

gets(bmp_name);

//printf(“\n%s”,bmp_name);

file=fopen(bmp_name,”rb+”); //打開一個文件進行讀寫操作。

–times;

if (file==NULL)

{

printf(“\nerror opening %s for reading! “,bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf(“\nsorry, shutdown!”);

exit(1);

}

//讀取圖像信息

fseek(file,0L,0); //讀取圖像文件類型

fread(bmp,sizeof(BMP),1,file);

printf(“\n bmp tpye: %u”,bmp.bfType);

printf(“\n bmp size: %u”,bmp.bfSize);

printf(“\n bmp reserved1: %u”,bmp.bfReserved1);

printf(“\n bmp reserved2: %u”,bmp.bfReserved2);

printf(“\n bmp offBits: %u”,bmp.bfOffBits);

printf(“\n bmp bisize: %u”,bmp.biSize);

printf(“\n bmp biWidth: %d”,bmp.biWidth);

printf(“\n bmp biHeight: %d”,bmp.biHeight);

printf(“\n bmp biplans: %u”,bmp.biPlanes);

printf(“\n bmp biBitCount: %u”,bmp.biBitCount);

printf(“\n bmp biCompression: %u”,bmp.biCompression);

printf(“\n bmp biSizeImage: %u”,bmp.biSizeImage);

printf(“\n bmp biXPelsPerMeter: %d”,bmp.biXPelsPerMeter);

printf(“\n bmp biYPelsPerMeter: %d”,bmp.biYPelsPerMeter);

printf(“\n bmp biClrUsed: %u”,bmp.biClrUsed);

printf(“\n bmp biClrImportant: %u\n”,bmp.biClrImportant);

line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //獲得圖像數據每行的數據個數

//printf(“dfsa%u”,bmp.line_byte);

//bmp.imagedata=NULL;

imagedata=(unsigned char*)malloc(bmp.biSizeImage);

fseek(file,(long)bmp.bfOffBits,0);

fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//保存圖像

void image_save(FILE *file)

{

int times=3; //輸入文件名次數。

char bmp_name[10]; //文件名

//int i; //記錄數據區個數

printf(“\nplease enter a file name for writeing:”);

do

{

if (times3)

{

printf(“\nplease enter a file name for writeing again:”);

}

fflush(stdin);

gets(bmp_name);

printf(“\n%s”,bmp_name);

file=fopen(bmp_name,”wb+”); //打開一個文件進行讀寫操作。

–times;

if (file==NULL)

{

printf(“\nerror opening %s for writing”,bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf(“\nsorry, shutdown!”);

exit(1);

}

//寫文件頭

printf(“\n%s”,bmp_name);

fseek(file,0L,0); //圖像文件類型

fwrite((bmp.bfType),sizeof(short),1,file);

printf(“\n bmp tpye: %d”,bmp.bfType);

fseek(file,2L,0); //圖像文件大小

fwrite((bmp.bfSize),sizeof(int),1,file);

printf(“\n bmp size: %d”,bmp.bfSize);

fseek(file,6L,0); //圖像文件保留字1

fwrite((bmp.bfReserved1),sizeof(short),1,file);

printf(“\n bmp reserved1: %d”,bmp.bfReserved1);

fseek(file,8L,0); //圖像文件保留字2

fwrite((bmp.bfReserved2),sizeof(short),1,file);

printf(“\n bmp reserved2: %d”,bmp.bfReserved2);

fseek(file,10L,0);//數據區的偏移量

fwrite((bmp.bfOffBits),sizeof(short),1,file);

printf(“\n bmp offBits: %d”,bmp.bfOffBits);

fseek(file,14L,0);//文件頭結構大小

fwrite((bmp.biSize),sizeof(int),1,file);

printf(“\n bmp bisize: %d”,bmp.biSize);

fseek(file,18L,0);//圖像的寬度

fwrite((bmp.biWidth),sizeof(int),1,file);

printf(“\n bmp biWidth: %d”,bmp.biWidth);

fseek(file,22L,0);//圖像的高度

fwrite((bmp.biHeight),sizeof(int),1,file);

printf(“\n bmp biHeight: %d”,bmp.biHeight);

fseek(file,24L,0);//圖像的面數

fwrite((bmp.biPlanes),sizeof(short),1,file);

printf(“\n bmp biplans: %d”,bmp.biPlanes);

fseek(file,28L,0);//圖像一個像素的字節數

fwrite((bmp.biBitCount),sizeof(short),1,file);

printf(“\n bmp biBitCount: %d”,bmp.biBitCount);

fseek(file,30L,0);//圖像壓縮信息

fwrite((bmp.biCompression),sizeof(short),1,file);

printf(“\n bmp biCompression: %d”,bmp.biCompression);

fseek(file,34L,0);//圖像數據區的大小

fwrite((bmp.biSizeImage),sizeof(int),1,file);

printf(“\n bmp biSizeImage: %d”,bmp.biSizeImage);

fseek(file,38L,0);//水平分辨率

fwrite((bmp.biXPelsPerMeter),sizeof(int),1,file);

printf(“\n bmp biXPelsPerMeter: %d”,bmp.biXPelsPerMeter);

fseek(file,42L,0);//垂直分辨率

fwrite((bmp.biYPelsPerMeter),sizeof(int),1,file);

printf(“\n bmp biYPelsPerMeter: %d”,bmp.biYPelsPerMeter);

fseek(file,46L,0);//顏色索引數

fwrite((bmp.biClrUsed),sizeof(int),1,file);

printf(“\n bmp biClrUsed: %d”,bmp.biClrUsed);

fseek(file,50L,0);//重要顏色索引數

fwrite((bmp.biClrImportant),sizeof(int),1,file);

printf(“\n bmp biClrImportant: %d\n”,bmp.biClrImportant);

fseek(file,(long)(bmp.bfOffBits),0);

fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//pixProcess.c文件

#includestdio.h

#includestdlib.h

#includemath.h

#include”image.h”

//灰度化

void image_gray()

{

int i,j;

unsigned char tmp;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));

imagedata[i*line_byte+j*3+0]=tmp;

imagedata[i*line_byte+j*3+1]=tmp;

imagedata[i*line_byte+j*3+2]=tmp;

//printf(“\nnidsfh%d %d”,i,j);

}

}

}

//二值化

void image_binarization()

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if ((*(imagedata+i*line_byte+j))128)

{

imagedata[i*line_byte+j]=0;

}

else

{

imagedata[i*line_byte+j]=255;

}

}

}

}

void image_opposite() //反相

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);

}

}

}

void image_channel() //抽取RGB通道

{

int i,j;

char rgb;

printf(“\nplease enter a char(r/g/b): “);

fflush(stdin);

scanf(“%c”,rgb);

if (rgb==’b’)

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j+1]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else if(rgb==’g’)

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+1]=0;

}

}

}

}

void image_bright()//改變圖像亮度

{

int level;

int i,j;

printf(“\n please enter the level of brightness[-255 to 255] :”);

fflush(stdin);

scanf(“%d”,level);

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if (level=0)

{

if ((imagedata[i*line_byte+j]+level)255)

imagedata[i*line_byte+j]=255;

else

imagedata[i*line_byte+j]+=level;

}

else

{

if ((imagedata[i*line_byte+j]-abs(level))0)

imagedata[i*line_byte+j]=0;

else

imagedata[i*line_byte+j]+=level;

}

}

}

}

//void image_create() //創建一幅24位BMP圖像文件。

//{

//main.c文件

#includestdio.h

#includestdlib.h

#includestring.h

#includeconio.h

#include”image.h”

BMP bmp;

int main()

{

FILE *file=NULL;

int choose;

char gono;

do

{

image_info(file); //imagedata已經分配了動態內存,但是沒有釋放

printf(“\n 1.image_opposite”);

printf(“\n 2.image_gray”);

printf(“\n 3.image_binarization”);

printf(“\n 4.image_channel”);

printf(“\n 5.image_brightness”);

//printf(“6.image_opposite”);

//printf(“7.image_opposite”);

printf(“\nchoose your options:”);

fflush(stdin);

scanf(“%d”,choose);

switch(choose)

{

case 1:

image_opposite();

image_save(file);

free(imagedata);

break;

case 2:

image_gray();

image_save(file);

free(imagedata);

break;

case 3:

image_binarization();

image_save(file);

free(imagedata);

break;

case 4:

image_channel();

image_save(file);

free(imagedata);

break;

case 5:

image_bright();

image_save(file);

free(imagedata);

break;

default:

printf(“\n wrong choose!”);

}

printf(“\nlet’s go on?(y/n):”);

fflush(stdin);

scanf(“%c”,gono);

if (gono==’n’)

{

printf(“\nbye bye!”);

break;

}

}

while(1);

return 0;

}

利用c語言怎樣對bmp圖像進行平移的操作?

位圖平移沒有這方面的庫函數,必須自己來實現,下面是位圖平移的參考代碼:

#include “stdafx.h”

#includewindows.h

#includestdio.h

#includemath.h

int _tmain(int argc, _TCHAR* argv[])

{

int width;

int height;

RGBQUAD *pTableColor;

unsigned char *pBmpBuf1,*pBmpBuf2;

BITMAPFILEHEADER bfhead;

BITMAPINFOHEADER bihead;

//讀出源圖像的信息

FILE *fpr=fopen(“E:\\picture\\dog.bmp”,”rb”);

if(fpr==0)

return 0;

fread(bfhead,14,1,fpr);

fread(bihead,40,1,fpr);

width=bihead.biWidth;

height=bihead.biHeight;

int LineByte=(width*8/8+3)/4*4;

pTableColor=new RGBQUAD[256];

fread(pTableColor,sizeof(RGBQUAD),256,fpr);

pBmpBuf1=new unsigned char[LineByte*height];

fread(pBmpBuf1,LineByte*height,1,fpr);

fclose(fpr);

//將處理後的圖像賦值為白色

pBmpBuf2=new unsigned char[LineByte*height];

for(int i=0;iheight;i++)

for(int j=0;jwidth;j++)

{

unsigned char *p;

p=(unsigned char*)(pBmpBuf2+LineByte*i+j);

(*p)=255;

}

//左右平移功能的實現

int t;

printf(“請輸入左平移或右平移的大小t(左移t0,右移t0):”);

scanf(“%d”,t);

int k=abs(t);

printf(“%d”,k);

if(t0)

{

if(t=(-width))

{

for(int i=0;iheight;i++)

for(int j=0;j(width-k);j++)

{

unsigned char *p1,*p2;

p1=pBmpBuf1+LineByte*i+j+k;

p2=pBmpBuf2+LineByte*i+j;

(*p2)=(*p1);

}

}

}

else

{

if(t=width)

{

for(int i=0;iheight;i++)

for(int j=k;jwidth;j++)

{

unsigned char *p1,*p2;

p1=pBmpBuf1+LineByte*i+j-k;

p2=pBmpBuf2+LineByte*i+j;

(*p2)=(*p1);

}

}

}

//保存處理後的圖像

FILE *fpw=fopen(“dog.bmp”,”wb”);

fwrite(bfhead,14,1,fpw);

fwrite(bihead,40,1,fpw);

fwrite(pTableColor,sizeof(RGBQUAD),256,fpw);

fwrite(pBmpBuf2,LineByte*height,1,fpw);

fclose(fpw);

return 0;

}

原創文章,作者:WJAM,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/145155.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
WJAM的頭像WJAM
上一篇 2024-10-26 11:55
下一篇 2024-10-26 11:55

相關推薦

  • 如何在Java中拼接OBJ格式的文件並生成完整的圖像

    OBJ格式是一種用於表示3D對象的標準格式,通常由一組頂點、面和紋理映射坐標組成。在本文中,我們將討論如何將多個OBJ文件拼接在一起,生成一個完整的3D模型。 一、讀取OBJ文件 …

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • 如何實現圖像粘貼到蒙版

    本文將從多個方面介紹圖像粘貼到蒙版的實現方法。 一、創建蒙版 首先,在HTML中創建一個蒙版元素,用於接收要粘貼的圖片。 <div id=”mask” style=”widt…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • 從不同位置觀察同一個物體,看到的圖形一定不同

    無論是在平時的生活中,還是在科學研究中,都會涉及到觀察物體的問題。而我們不僅要觀察物體本身,還需要考慮觀察的位置對觀察結果的影響。從不同位置觀察同一個物體,看到的圖形一定不同。接下…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28

發表回復

登錄後才能評論