HDC hdcDest, // 位图显示目标设备环境中
int nXDest, // 位图显示在客户区的x坐标
int nYDest, // 位图显示在客户区的y坐标
int nWidth, // 位图显示的宽度
int nHeight, // 位图显示的长度
HDC hdcSrc, // 源设备环境(包含需要显示的bmp位图)
int nXSrc, // 在当前位图中显示的开始x位置
int nYSrc, // 在当前位图中显示的开始y位置
DWORD dwRop // 映射模式
) //显示bmp位图
#include<windows.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)
}
最好在VC中编译
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位图#include<windows.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创建的项目 , 里面包含相应资源
直接上代码吧:#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
unsigned char *pBmpBuf//读入图像数据的指针
int bmpWidth//图像的宽
int bmpHeight//图像的高
RGBQUAD *pColorTable//颜色表指针
int biBitCount//图像类型,每像素位数
bool readBmp(char *bmpName)
{
//二进制读方式打开指定的图像文件
FILE *fp=fopen(bmpName,"rb")
if(fp==0) return 0
//跳过位图文件头结构BITMAPFILEHEADER
fseek(fp, sizeof(BITMAPFILEHEADER),0)
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
BITMAPINFOHEADER head
fread(&head, sizeof(BITMAPINFOHEADER), 1,fp)
//获取图像宽、高、每像素所占位数等信息
bmpWidth = head.biWidth
bmpHeight = head.biHeight
biBitCount = head.biBitCount
//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
int lineByte=(bmpWidth * biBitCount/8+3)/4*4
//灰度图像有颜色表,且颜色表表项为256
if(biBitCount==8){
//申请颜色表所需要的空间,读颜色表进内存
pColorTable=new RGBQUAD[256]
fread(pColorTable,sizeof(RGBQUAD),256,fp)
}
//申请位图数据所需要的空间,读位图数据进内存
pBmpBuf=new unsigned char[lineByte * bmpHeight]
fread(pBmpBuf,1,lineByte * bmpHeight,fp)
//关闭文件
fclose(fp)
return 1
}
bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height,
int biBitCount, RGBQUAD *pColorTable)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if(!imgBuf)
return 0
//颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
int colorTablesize=0
if(biBitCount==8)
colorTablesize=1024
//待存储图像数据每行字节数为4的倍数
int lineByte=(width * biBitCount/8+3)/4*4
//以二进制写的方式打开文件
FILE *fp=fopen(bmpName,"wb")
if(fp==0) return 0
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead
fileHead.bfType = 0x4D42//bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ colorTablesize + lineByte*height
fileHead.bfReserved1 = 0
fileHead.bfReserved2 = 0
//bfOffBits是图像文件前3个部分所需空间之和
fileHead.bfOffBits=54+colorTablesize
//写文件头进文件
fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp)
//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADER head
head.biBitCount=biBitCount
head.biClrImportant=0
head.biClrUsed=0
head.biCompression=0
head.biHeight=height
head.biPlanes=1
head.biSize=40
head.biSizeImage=lineByte*height
head.biWidth=width
head.biXPelsPerMeter=0
head.biYPelsPerMeter=0
//写位图信息头进内存
fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp)
//如果灰度图像,有颜色表,写入文件
if(biBitCount==8)
fwrite(pColorTable, sizeof(RGBQUAD),256, fp)
//写位图数据进文件
fwrite(imgBuf, height*lineByte, 1, fp)
//关闭文件
fclose(fp)
return 1
}
int main()
{
char inFileName[90],outFileName[90]
printf("请输入原始位图文件的文件名:")
scanf("%s",inFileName)
printf("请输入加密程序产生的新位图文件的文件名:")
scanf("%s",outFileName)
//读入指定BMP文件进内存
readBmp(inFileName)
//输出图像的信息
printf("width=%d,height=%d, biBitCount=%d\n",bmpWidth,bmpHeight, biBitCount)
//将图像数据存盘
saveBmp(outFileName, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable)
//清除缓冲区,pBmpBuf和pColorTable是全局变量,在文件读入时申请的空间
delete []pBmpBuf
if(biBitCount==8)
delete []pColorTable
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)