读取txt

// 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();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值