Win32 C/C++ 从内存缓冲区加载图像

Win32 C/C++ Load Image from memory buffer(Win32 C/C++ 从内存缓冲区加载图像)

本文介绍了Win32 C/C++ 从内存缓冲区加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Win32 应用程序上加载图像 (.bmp) 文件,但我不想使用 Windows API 中的标准 LoadBitmap/LoadImage:我希望它从内存中已有的缓冲区加载.我可以轻松地直接从文件加载位图并将其打印在屏幕上,但这个问题让我卡住了.

I want to load an image (.bmp) file on a Win32 application, but I do not want to use the standard LoadBitmap/LoadImage from Windows API: I want it to load from a buffer that is already in memory. I can easily load a bitmap directly from a file and print it on the screen, but this issue is making me stuck.

我正在寻找的是一个像这样工作的函数:

What I'm looking for is a function that works like this:

HBITMAP LoadBitmapFromBuffer(char* buffer, int width, int height);

推荐答案

没关系,我找到了我的解决方案!这是初始化代码:

Nevermind, I found my solution! Here's the initializing code:

std::ifstream is;
is.open("Image.bmp", std::ios::binary);
is.seekg (0, std::ios::end);
length = is.tellg();
is.seekg (0, std::ios::beg);
pBuffer = new char [length];
is.read (pBuffer,length);
is.close();

tagBITMAPFILEHEADER bfh = *(tagBITMAPFILEHEADER*)pBuffer;
tagBITMAPINFOHEADER bih = *(tagBITMAPINFOHEADER*)(pBuffer+sizeof(tagBITMAPFILEHEADER));
RGBQUAD             rgb = *(RGBQUAD*)(pBuffer+sizeof(tagBITMAPFILEHEADER)+sizeof(tagBITMAPINFOHEADER));

BITMAPINFO bi;
bi.bmiColors[0] = rgb;
bi.bmiHeader = bih;

char* pPixels = (pBuffer+bfh.bfOffBits);

char* ppvBits;

hBitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**) &ppvBits, NULL, 0);
SetDIBits(NULL, hBitmap, 0, bih.biHeight, pPixels, &bi, DIB_RGB_COLORS);

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

这篇关于Win32 C/C++ 从内存缓冲区加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Win32 C/C++ 从内存缓冲区加载图像

基础教程推荐