當前位置:首頁 » 操作系統 » 點陣圖源碼

點陣圖源碼

發布時間: 2025-06-20 03:48:13

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

lz 你好


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

BOOLBitBlt(
HDChdcDest,//點陣圖顯示目標設備環境中
intnXDest,//點陣圖顯示在客戶區的x坐標
intnYDest,//點陣圖顯示在客戶區的y坐標
intnWidth,//點陣圖顯示的寬度
intnHeight,//點陣圖顯示的長度
HDChdcSrc,//源設備環境(包含需要顯示的bmp點陣圖)
intnXSrc,//在當前點陣圖中顯示的開始x位置
intnYSrc,//在當前點陣圖中顯示的開始y位置
DWORDdwRop//映射模式
);


以下是源代碼:

//顯示bmp點陣圖
#include<windows.h>
#include"resource.h"

LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
voidDrawBrick();

intWINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
PSTRszCmdLine,
intiCmdShow)
{
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("ThisprogramrequiresWindowsNT!"),
szAppName,MB_ICONERROR);
return0;
}

hwnd=CreateWindow(szAppName,
TEXT("BmpDemo"),
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);
}

returnmsg.wParam;
}

LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
static HBITMAP hBitmap; //點陣圖句柄標示點陣圖
staticint cxBitmap,cyBitmap; //點陣圖的長寬
BITMAP bitmap;
HDC hdc,hdcMem;
HINSTANCE hInstance;
PAINTSTRUCT ps;

switch(message)
{
caseWM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance; //獲取窗口的實例句柄

hBitmap=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1)); //將點陣圖載入到內存中

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

cxBitmap=bitmap.bmWidth;//獲取點陣圖的長
cyBitmap=bitmap.bmHeight;//獲取點陣圖的寬

return0;

caseWM_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);

return0;

caseWM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage(0);

return0;
}

returnDefWindowProc(hwnd,message,wParam,lParam);
}



程序運行效果:

Ⅱ 用c語言讀取24位點陣圖bmp文件

可以使用C語言標准函數庫中的fopen、fseek、fclose等系列函數來打開bmp點陣圖文件,以及進行相應的處理,下面是一個demo,僅供參考。以下代碼在vc6.0中編譯通過。


#include<stdio.h>
#include<stdlib.h>
#//ThebmpFileHeaderlengthis14
#defineBM19778//TheASCIIcodeforBM
/*Testthefileisbmpfileornot*/
voidbmpFileTest(FILE*fpbmp);
/**/
voidbmpHeaderPartLength(FILE*fpbmp);
/**/
voidBmpWidthHeight(FILE*fpbmp);
//getr,g,bdata
voidbmpDataPart(FILE*fpbmp);
//
voidbmpoutput(FILE*fpout);
unsignedintOffSet=0;//
longwidth;//TheWidthoftheDataPart
longheight;//TheHeightoftheDataPart
unsignedcharr[2000][2000],output_r[2000][2000];
unsignedcharg[2000][2000],output_g[2000][2000];
unsignedcharb[2000][2000],output_b[2000][2000];
intmain(intargc,char*argv[])
{
/*Openbmpfile*/
unsignedchar*fp_temp;
FILE*fpbmp;
FILE*fpout;
fpbmp=fopen("1.bmp","rb");
if(fpbmp==NULL)
{
printf("Openbmpfailed!!! ");
return1;
}
fpout=fopen("out.bmp","wb+");
if(fpout==NULL)
{
printf("Openout.bmpfailed!!! ");
return1;
}

bmpFileTest(fpbmp);//Testthefileisbmpfileornot
bmpHeaderPartLength(fpbmp);//GetthelengthofHeaderPart
BmpWidthHeight(fpbmp);//


//
fseek(fpbmp,0L,SEEK_SET);
fseek(fpout,0L,SEEK_SET);

fp_temp=(unsignedchar*)malloc(OffSet);
fread(fp_temp,1,OffSet,fpbmp);
fwrite(fp_temp,1,OffSet,fpout);

bmpDataPart(fpbmp);//Reservethedatatofile

/*


如果您想對圖片進行處理,請您再這里插入處理函數!!!!!!!!!!!!!!!!!!

*/
bmpoutput(fpout);
fclose(fpbmp);
fclose(fpout);
return0;
}
voidbmpoutput(FILE*fpout)
{
inti,j=0;
intstride;
unsignedchar*pixout=NULL;

stride=(24*width+31)/8;
stride=stride/4*4;
pixout=(unsignedchar*)malloc(stride);

fseek(fpout,OffSet,SEEK_SET);
for(j=0;j<height;j++)
{
for(i=0;i<width;i++)
{
pixout[i*3+2]=output_r[height-1-j][i];
pixout[i*3+1]=output_g[height-1-j][i];
pixout[i*3]=output_b[height-1-j][i];
}
fwrite(pixout,1,stride,fpout);
}
}
voidbmpDataPart(FILE*fpbmp)
{
inti,j=0;
intstride;
unsignedchar*pix=NULL;
FILE*fpr;
FILE*fpg;
FILE*fpb;

if((fpr=fopen("bmpr.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpr.txt.!!!");
exit(1);
}
if((fpg=fopen("bmpg.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpg.txt.!!!");
exit(1);
}
if((fpb=fopen("bmpb.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpb.txt.!!!");
exit(1);
}

fseek(fpbmp,OffSet,SEEK_SET);
stride=(24*width+31)/8;
stride=stride/4*4;
pix=(unsignedchar*)malloc(stride);

for(j=0;j<height;j++)
{
fread(pix,1,stride,fpbmp);
for(i=0;i<width;i++)
{
r[height-1-j][i]=pix[i*3+2];
g[height-1-j][i]=pix[i*3+1];
b[height-1-j][i]=pix[i*3];
output_r[height-1-j][i]=pix[i*3+2];
output_g[height-1-j][i]=pix[i*3+1];
output_b[height-1-j][i]=pix[i*3];
}
}
for(i=0;i<height;i++)
{
for(j=0;j<width-1;j++)
{
fprintf(fpb,"%4d",b[i][j]);
fprintf(fpg,"%4d",g[i][j]);
fprintf(fpr,"%4d",r[i][j]);
}
fprintf(fpb,"%4d ",b[i][j]);
fprintf(fpg,"%4d ",g[i][j]);
fprintf(fpr,"%4d ",r[i][j]);
}

fclose(fpr);
fclose(fpg);
fclose(fpb);

}
voidbmpFileTest(FILE*fpbmp)
{
unsignedshortbfType=0;

fseek(fpbmp,0L,SEEK_SET);//seek_set起始位置
fread(&bfType,sizeof(char),2,fpbmp);
if(BM!=bfType)
{
printf("Thisfileisnotbmpfile.!!! ");
exit(1);
}
}
/**/
voidbmpHeaderPartLength(FILE*fpbmp)
{
fseek(fpbmp,10L,SEEK_SET);
fread(&OffSet,sizeof(char),4,fpbmp);
printf("TheHeaderPartisoflength%d. ",OffSet);
}
/**/
voidBmpWidthHeight(FILE*fpbmp)
{
fseek(fpbmp,18L,SEEK_SET);
fread(&width,sizeof(char),4,fpbmp);
fseek(fpbmp,22L,SEEK_SET);
fread(&height,sizeof(char),4,fpbmp);
printf("TheWidthofthebmpfileis%ld. ",width);
printf("TheHeightofthebmpfileis%ld. ",height);
}
熱點內容
奧德賽哪個配置帶魔術門 發布:2025-06-20 09:31:44 瀏覽:201
python中列表的長度 發布:2025-06-20 09:23:49 瀏覽:926
三子棋演算法 發布:2025-06-20 09:23:46 瀏覽:911
安卓軟體是什麼圖案 發布:2025-06-20 09:23:09 瀏覽:510
一句話木馬php 發布:2025-06-20 09:17:34 瀏覽:327
編譯原理分析模塊的組成 發布:2025-06-20 09:17:29 瀏覽:291
linuxmounton 發布:2025-06-20 09:17:22 瀏覽:420
工業演算法 發布:2025-06-20 09:12:15 瀏覽:10
伺服器主板又叫什麼 發布:2025-06-20 08:47:54 瀏覽:879
c語言怎麼編程 發布:2025-06-20 08:37:50 瀏覽:600