先放上运行结果:
可以发现红绿蓝是从后往前的。
必须以C++方式编译代码!
#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 bufsiz;BYTE* v;lck->GetDataPointer(&bufsiz, &v);puts("每个像素的 RGB 值:");for (UINT i = 0; i < bufsiz; i += 3) {printf("%-4hhu%-4hhu%-4hhu\n", v[i], v[i + 1], v[i + 2]);}lck->Release();bmp->Release();fram->Release();dec->Release();fac->Release();CoUninitialize();return 0;
}