// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#include<assert.h>
#pragma comment(lib, "gdiplus.lib")
using namespace std;
using namespace Gdiplus;
int main()
{
//第一种方法
//GdiplusStartupInput gdiplusStartupInput;
//ULONG_PTR gdiplusToken;
//GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
//Image* image = new Image(L"D:\\我的文档\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication2\\ipch\\FakePhoto.jpg");
//printf("The width of the image is %u.\n", image->GetWidth());
//printf("The height of the image is %u.\n", image->GetHeight());
//delete image;
//GdiplusShutdown(gdiplusToken);
//getchar();
//return 0;
第二种方法
//GdiplusStartupInput gdiplusstartupinput;
//ULONG_PTR gdiplustoken;
//GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
//
//wstring infilename(L"D:\\我的文档\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication2\\ipch\\FakePhoto.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());
//for (UINT y = 0; y < height; y++)
// for (UINT 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;
//图形设备接口+
GdiplusStartupInput gdiplusstartupinput; //图形设备接口加启动输入
ULONG_PTR gdiplustoken; //无符号型长指针
GdiplusStartup(&gdiplustoken,&gdiplusstartupinput,NULL);//图形设备接口加启动
wstring infilename(L"C:\\Users\\Administrator\\Desktop\\新建文件夹\\291158924.jpg"); //宽字节字符串地址,作为输入用
string outfilename("C:\\Users\Administrator\\Desktop\\新建文件夹\\像素.txt"); //字符串地址,作为输出用
Bitmap *bitmap = new Bitmap(infilename.c_str()); //位图文件
int width = bitmap->GetWidth(); //获取像素的宽
int height = bitmap->GetHeight(); //获取像素的高
cout << width << endl;
cout << height << endl;
int a = 78;
Color color;
//bitmap->GetPixel(1,1,&color);
//cout << to_string((int)color.GetRed()) + "," + to_string((int)color.GetGreen()) + "," + to_string((int)color.GetBlue())<< endl;
//开始输出像素值
int i = 0;
//ofstream fout(outfilename.c_str());
ofstream fout("C:\\Users\\Administrator\\Desktop\\新建文件夹\\像素.txt");
for (int y = 0; y < 50; y++) {
for (int x = 0; x < 50; x++) {
bitmap->GetPixel(x,y,&color);
//cout << i<<","<<to_string((int)color.GetRed()) + "," + to_string((int)color.GetGreen()) + "," + to_string((int)color.GetBlue())<< endl;
fout <<i<<":"<< (int)color.GetRed() << "," << (int)color.GetGreen() << "," << (int)color.GetBlue() << endl;
i++;
}
}
fout.close();
delete bitmap;
GdiplusShutdown(gdiplustoken);
getchar();
}
void ReadText(string file) {
ifstream in("C:\\Users\\Administrator\\Desktop\\测试.txt");
string line;
if (in) {
while (getline(in, line)) {
cout << line << endl;
}
}
getchar();
in.close();
}