1、将bin目录拷贝至工程目录下
2、程序中添加相关头文件和库文件
#include "binpng.h"
#include "binzlib.h"
#include "binwsGDI.h"
#pragma comment(lib, "binlibpng16.lib")
#ifdef _DEBUG
#pragma comment(lib, "binwsGDI_d.lib")
#else
#pragma comment(lib, "binwsGDI.lib")
#endif
3、将libpng16.dll动态链接库文件拷贝到工程目录。
4、读取PNG文件,绘制PNG文件,生成新的PNG文件
可以将下列代码放置到一个按钮的单击事件里。
png_image image; memset(&image, 0, (sizeof image)); image.version = PNG_IMAGE_VERSION; if (png_image_begin_read_from_file(&image, "test.png") != 0) { png_bytep buffer; image.format = PNG_FORMAT_RGBA; buffer = (png_bytep)malloc(PNG_IMAGE_SIZE(image)); if (buffer != NULL && png_image_finish_read(&image, NULL, buffer, 0, NULL) != 0) { int iWidth = image.width; int iHeight = image.height; TCHAR tszInfo[100] = {0}; _stprintf(tszInfo, _T("宽:%d 高:%d"), iWidth, iHeight); AfxMessageBox(tszInfo); // 绘制到界面上 unsigned char* pData = (unsigned char*)buffer; HDC hdc = ::GetDC(m_hWnd); CwsGDI::DrawDCByData(hdc, 0, 0, iWidth, iHeight, pData); if (png_image_write_to_file(&image, "test2.png", 0, buffer, 0, NULL) != 0) { AfxMessageBox(L"生成新的图片成功"); } } else { if (buffer == NULL) png_image_free(&image); else free(buffer); } } |
评论