图像像素点颜色获取+保存图片代码

2022-07-15 03:14:25   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《图像像素点颜色获取+保存图片代码》,欢迎阅读!
像素,图像,获取,保存,颜色



#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.dywdw.cn/50cd97b29b6648d7c1c746f1.html

相关推荐
推荐阅读