GDI+ 读取(jpg,PNG,BMP)等格式)图片每个像素的值

来源:互联网 发布:客户满意率的数据来源 编辑:程序博客网 时间:2024/05/18 03:43

感觉作为一个学图形学的,还是可能偶尔要亲自读取纹理的,这里有篇技术博客介绍用GDI+读取图片像素

原博客地址:http://blog.csdn.net/wangyaninglm/article/details/41868599



#include "stdafx.h"  #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());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;}



BYTE *TextureBuffer;void LoadTexture(wstring TexureFilename){Gdiplus::GdiplusStartupInput gdiplusstartupinput;Gdiplus::Color color;ULONG_PTR gdiplustoken;Gdiplus::GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL); //读取纹理 texture= new Gdiplus::Bitmap(TexureFilename.c_str()); //获取纹理分辨率的宽度和高度 texture_h = texture->GetHeight(); texture_w = texture->GetWidth(); //创建纹理缓存,screen_h*screen_w * 3个字节大 TextureBuffer = new BYTE[texture_h*texture_w * 3]; for (int i = 0; i < texture_h; ++i) { for (int j = 0; j <texture_w; ++j) { //获取相应数组[j][i]的像素,注意这里下标从0开始 texture->GetPixel(j, i, &color); //将像素的颜色输入纹理缓存,注意颜色的顺序应该是BGR TextureBuffer[i * texture_w * 3 + (j + 1) * 3 - 1] = color.GetR(); TextureBuffer[i * texture_w * 3 + (j + 1) * 3 - 2] = color.GetG(); TextureBuffer[i * texture_w * 3 + (j + 1) * 3 - 3] = color.GetB(); } }  Gdiplus::GdiplusShutdown(gdiplustoken);}


0 0
原创粉丝点击