读取图片并显示每个像素处的RGB值

      不记得在网上什么地方见到的demo了,感觉会有时候会用到。就是读取一幅图片每个像素处的RGB三个通道的值。代码如下,代码很简单,就不解释了,主要是那个gdi中的函数用的少:

#include 

#include 

#include 

#include 

#include 

#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"<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;

}

      源代码下载:下载地址

你可能感兴趣的:(C++,GDI,像素RGB)