先放上运行结果:
可以发现红绿蓝是从后往前的。
必须以C++方式编译代码!
// 参考资料:https://learn.microsoft.com/zh-cn/windows/win32/wic/-wic-lh
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <wincodec.h>
int main(void)
{
CoInitialize(nullptr);
IWICImagingFactory* fac;
CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fac));
IWICBitmapDecoder* dec;
fac->CreateDecoderFromFilename(L"test.bmp", nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &dec);
IWICBitmapFrameDecode* fram;
dec->GetFrame(0, &fram);
IWICBitmap* bmp;
fac->CreateBitmapFromSource(fram, WICBitmapCacheOnDemand, &bmp);
UINT w, h;
bmp->GetSize(&w, &h);
printf("宽 %u 高 %u\n", w, h);
IWICBitmapLock* lck;
WICRect rc = { .X = 0, .Y = 0, .Width = (INT)w, .Height = (INT)h };
bmp->Lock(nullptr, WICBitmapLockRead, &lck);
UINT stride;
lck->GetStride(&stride);
printf("步长 %u\n", stride);
UINT bufsiz;
BYTE* v;
lck->GetDataPointer(&bufsiz, &v);
puts("每个像素的 RGB 值:");
for (UINT i = 0; i < h; i++) {
printf("第 %u 行\n", i);
BYTE* row = v + (UINT64)i * stride;
for (UINT j = 0; j < w; j++) {
BYTE* p = row + 3 * j;
printf("%-4hhu%-4hhu%-4hhu\n", p[0], p[1], p[2]);
}
putchar('\n');
}
lck->Release();
bmp->Release();
fram->Release();
dec->Release();
fac->Release();
CoUninitialize();
return 0;
}