gdiplus 从内存加载绘制图片
需求:通过http请求从nginx下载jpg图片,在本地窗口用gdiplus显示
通过http直接访问图片url,返回的jpg图片以字符串形式存在string对象中,可以先写为本地文件,再用gdiplus的FromFile
函数从文件加载图片,缺点不言而喻,多了磁盘读写,还要处理图片更新删除问题,code:
// http请求部分
string data; // http response body
// 保存到文件
std::ofstream ofs(m_img_path, std::ofstream::binary);
ofs.write(data.c_str(), data.size());
ofs.close();
// ui绘制部分,需要在OnPaint中调用
Gdiplus::Graphics gwnd(GetSafeHwnd());
CRect rc;
GetClientRect(&rc); // 计算当前客户区,使得重绘时图片随窗口缩放
Gdiplus::Bitmap * lpBmp = Gdiplus::Bitmap::FromFile(m_preview_img.c_str());
Gdiplus::Status status = gwnd.DrawImage(lpBmp, rc.left, rc.top, rc.Width(), rc.Height());
if (status != Gdiplus::Status::Ok)
{
loginfo("图片显示失败");
}
delete lpBmp;
直接从内存加载图片(你永远可以相信stackoverflow)注意
// http请求部分
string m_preview_img // http response body
// 从string构建gdi+ image
IStream* stream = SHCreateMemStream((BYTE *)m_preview_img.data(), m_preview_img.size());
Gdiplus::Image *image = Gdiplus::Image::FromStream(stream);
// 计算绘制位置
Gdiplus::Graphics gwnd(GetSafeHwnd());
CRect rc;
GetClientRect(&rc);
// 绘制图片
Gdiplus::Status status = gwnd.DrawImage(image, rc.left, rc.top, rc.Width(), rc.Height());
if (status != Gdiplus::Status::Ok)
{
loginfo("图片显示失败");
}
stream->Release();
delete image;
分类:
C/C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
2019-04-12 ES6学习笔记(十八)Class 的继承