【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《图像像素点颜色获取+保存图片代码》,欢迎阅读!
#include #include #include
#include #include
#pragma comment(lib, "gdiplus.lib")
using namespace std; using namespace Gdiplus;
int GetEncoderClsid(const WCHAR* format, LSID* pClsid) {
UINT num = 0; UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size); if(size == 0) return -1;
pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); if(pImageCodecInfo == NULL) return -1;
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j) {
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) {
*pClsid = pImageCodecInfo[j].Clsid; free(pImageCodecInfo); return j; } }
free(pImageCodecInfo); return -1; }
int main() {
GdiplusStartupInput gdiplusstartupinput; ULONG_PTR gdiplustoken;
GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr);
wstring infilename(L"shadow.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++)//height for (int x = 0; x < width; x++)//width {
int grey;//灰度值
bmp->GetPixel(x, y, &color);
grey=((int)color.GetRed()*30+(int)color.GetGreen()*59+(int)color.GetBlue()*11)/100; //灰度值计算
fout << x << ";" << y << ";" < << (int)color.GetRed() << ";" << (int)color.GetGreen() << ";"
<< (int)color.GetBlue() << ";" <
} CLSID pngClsid; GetEncoderClsid(L"image/jpeg", &pngClsid); bmp->Save(L"change.jpg", &pngClsid); //保存图片 fout.close();
delete bmp;
GdiplusShutdown(gdiplustoken); return 0; }
本文来源:https://www.wddqxz.cn/50cd97b29b6648d7c1c746f1.html