gdi显示bmp文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gdi显示bmp文件相关的知识,希望对你有一定的参考价值。

/*
 * gdi显示bmp文件
 */
#include "windows.h"
#include "stdio.h"

#define		CLASSNAME		"GDI_BMPPLAYER"
#define		TITLENAME		"GDI_BMPPLAYER"

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    switch(message)
    {
		case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }break;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}

int main(int argc,char *argv[])
{
	if(argc != 2)
	{
		printf("gdi_bmpplayer.exe [bmpfile]\n");
		return -1;
	}

	WNDCLASS wndclass;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=NULL;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName=CLASSNAME;
    if(!RegisterClass(&wndclass))
    {
        return FALSE;
    }
    HWND hwnd = CreateWindow(CLASSNAME,TITLENAME,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
        800,600,NULL,NULL,NULL,NULL);
    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

	HDC hdcBuf = CreateCompatibleDC(NULL);
    HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,argv[1],  
        IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);  
    BITMAP bitmap;
    GetObject(hBitmap,sizeof(bitmap),&bitmap);
    SelectObject(hdcBuf, hBitmap);

	MSG msg;GetMessage(&msg,NULL,0,0);
	while(msg.message != WM_QUIT)
    {
        if(PeekMessage( &msg,NULL,0,0,PM_REMOVE))
        {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
        }
		else
		{
			HDC hdc = GetDC(hwnd);
			BitBlt(hdc, 0, 0, bitmap.bmWidth,bitmap.bmHeight, hdcBuf, 0, 0, SRCCOPY); 
			ReleaseDC(hwnd,hdc);
		}
	}

    DeleteObject(hBitmap);
    DeleteDC(hdcBuf);
	return msg.wParam;
}

 

以上是关于gdi显示bmp文件的主要内容,如果未能解决你的问题,请参考以下文章

C# 控制台应用程序 - 如何使用 GDI+ 在 BMP/JPG 文件中绘图?

windows gdi截图保存bmp

图片转换,PNG转32位BMP;BMP大小转换

GDI+ 支持的图片文件格式

用VC++ 怎样把图像输出保存成jepg或bmp图像

delphi:怎么把JPEG、BMP,wmf等Timage支持的格式转化为TIFF?