位图源码
发布时间: 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);
}
热点内容