不记得在网上什么地方见到的demo了,感觉会有时候会用到。就是读取一幅图片每个像素处的RGB三个通道的值。代码如下,代码很简单,就不解释了,主要是那个gdi中的函数用的少:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace std;
using namespace Gdiplus;
int main()
{
GdiplusStartupInput gdiplusstartupinput;
ULONG_PTR gdiplustoken;
GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
wstring infilename(L"1.jpg");
string outfilename("color.txt");
//读图片
Bitmap* bmp = new Bitmap(infilename.c_str());
UINT height = bmp->GetHeight();
UINT width = bmp->GetWidth();
cout << "width " << width << ", height " << height << endl;
Color color;
ofstream fout(outfilename.c_str());
fout<<"structure"<<endl;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
bmp->GetPixel(x, y, &color);
fout << x << ";" << y << ";"
<< (int)color.GetRed() << ";"
<< (int)color.GetGreen() << ";"
<< (int)color.GetBlue() << endl;
}
fout.close();
delete bmp;
GdiplusShutdown(gdiplustoken);
return 0;
}
源代码下载:下载地址