VC++打开8bit/pixel的bmp图像程序怎么实现啊?

可以写具体C++代码,告诉具体的思路也行!谢谢哈~急、急... 可以写具体C++代码,告诉具体的思路也行!谢谢哈~急、急 展开
 我来答
ChikaIto
推荐于2016-06-26 · TA获得超过326个赞
知道小有建树答主
回答量:205
采纳率:0%
帮助的人:0
展开全部
8bit的bmp图像 就是那种带调色板的256色图像?

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <tchar.h>
#include <shlobj.h>

typedef struct tagPALETTE // 调色板
{
BYTE b; // blue
BYTE g; // green
BYTE r; // red
BYTE n; // not support
} PALETTE, *LPPALETTE;

HBITMAP hBitmap; // 位图句柄
SIZE bmSize; // 尺寸

BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
wchar_t path[MAX_PATH];

// 取得放在桌面的8位BMP图像路径
SHGetSpecialFolderPath(NULL, path, CSIDL_DESKTOPDIRECTORY, FALSE);
_tcscat(path, L"\\test.bmp");

// 打开图像
FILE *fp = _tfopen(path, L"rb");

if (!fp) return FALSE;

BITMAPFILEHEADER bmHeader; // 文件头
BITMAPINFOHEADER bmInfohd; // 信息头
PALETTE pal[256]; // 调色板

fread(&bmHeader, sizeof(BITMAPFILEHEADER), 1, fp); // 读文件头
fread(&bmInfohd, sizeof(BITMAPINFOHEADER), 1, fp); // 读信息头
fread(&pal, sizeof(pal), 1, fp); // 读调色板

bmSize.cx = bmInfohd.biWidth;
bmSize.cy = bmInfohd.biHeight;

INT pxLen = bmInfohd.biWidth * bmInfohd.biHeight;
BYTE *pixels = new BYTE[pxLen];

fread(pixels, pxLen, 1, fp); // 读取像素
fclose(fp); // 关闭文件

BYTE *dstPixels = new BYTE[pxLen * 3];

// 格式转换
for (INT i = 0, j = 0; i < pxLen; i++)
{
dstPixels[j++] = pal[pixels[i]].b;
dstPixels[j++] = pal[pixels[i]].g;
dstPixels[j++] = pal[pixels[i]].r;
}

delete [] pixels;

BITMAPINFOHEADER bmInfHeader;

bmInfHeader.biSize = sizeof (BITMAPINFOHEADER);
bmInfHeader.biWidth = bmInfohd.biWidth;
bmInfHeader.biHeight = bmInfohd.biHeight;
bmInfHeader.biSizeImage = 0;
bmInfHeader.biPlanes = 1;
bmInfHeader.biBitCount = 24;
bmInfHeader.biCompression = BI_RGB;
bmInfHeader.biXPelsPerMeter = 0;
bmInfHeader.biYPelsPerMeter = 0;
bmInfHeader.biClrUsed = 0;
bmInfHeader.biClrImportant = 0;

HDC hDC = GetWindowDC(hwnd);
hBitmap = CreateDIBitmap(hDC, &bmInfHeader, CBM_INIT, dstPixels, (BITMAPINFO*)&bmInfHeader, DIB_RGB_COLORS); // 创建DIB

ReleaseDC(hwnd, hDC);

delete [] dstPixels;

return TRUE;
}

VOID OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hwnd, &ps);
HDC hMemDC = CreateCompatibleDC(hDC);

SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, bmSize.cx, bmSize.cy, hMemDC, 0, 0, SRCCOPY); // 拷贝到窗口
DeleteDC(hMemDC);
EndPaint(hwnd, &ps);
}

VOID OnDestroy(HWND hwnd)
{
PostQuitMessage(0);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hWnd, WM_CREATE, OnCreate);
HANDLE_MSG(hWnd, WM_PAINT, OnPaint);
HANDLE_MSG(hWnd, WM_DESTROY, OnDestroy);

default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

return 0;
}

BOOL InitInstance(HINSTANCE hInstance, INT nCmdShow)
{
HWND hWnd = CreateWindow(TEXT("Simple"), TEXT("Simple"),
WS_OVERLAPPEDWINDOW,
100, 100, 500, 500, NULL, NULL, hInstance, NULL);

if (!hWnd) return FALSE;

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

ATOM WindowRegister(HINSTANCE hInstance)
{
WNDCLASSEX wnd;

wnd.cbClsExtra = 0;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.cbWndExtra = 0;
wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wnd.hCursor = LoadCursor(hInstance, IDC_ARROW);
wnd.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wnd.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
wnd.hInstance = hInstance;
wnd.lpfnWndProc = WndProc;
wnd.lpszClassName = TEXT("Simple");
wnd.lpszMenuName = NULL;
wnd.style = CS_HREDRAW | CS_VREDRAW;

return RegisterClassEx(&wnd);
}

INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, INT nShowCmd)
{
MSG msg;

WindowRegister(hInstance);

if (!InitInstance(hInstance, nShowCmd)) return FALSE;

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式