加载DLL中的图片资源生成Skia中的SkBitmap对象

最后更新于:2022-04-01 16:03:07

PPAPI Plugin在Windows下是DLL,可以嵌入图片文件,使用Skia绘图时需要根据DLL里的图片文件生成SkBitmap对象。下面是代码: ~~~ #include "utils.h" #include "SkStream.h" #include "SkImageDecoder.h" #include <tchar.h> SkBitmap* loadImageFromResource(UINT resId, LPCTSTR lpType) { TCHAR szLog[512] = { 0 }; HMODULE hModule = NULL; if (FALSE == GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS , (LPCTSTR)loadImageFromResource, &hModule)) { _stprintf_s(szLog, 512, _T("GetModuleHandleEx failed, error - %d\r\n"), GetLastError()); OutputDebugString(szLog); return NULL; } HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(resId), lpType); if (hRsrc == NULL) { _stprintf_s(szLog, 512, _T("FindResource failed, error - %d\r\n"), GetLastError()); OutputDebugString(szLog); return NULL; } HGLOBAL hImgData = LoadResource(hModule, hRsrc); if (hImgData == NULL) { _stprintf_s(szLog, 512, _T("LoadResource failed, error - %d\r\n"), GetLastError()); OutputDebugString(szLog); return NULL; } LPVOID lpData = LockResource(hImgData); if (lpData == NULL) { _stprintf_s(szLog, 512, _T("LockResource failed, error - %d\r\n"), GetLastError()); OutputDebugString(szLog); return NULL; } DWORD dwSize = SizeofResource(hModule, hRsrc); SkMemoryStream memoryStream(lpData, dwSize); SkImageDecoder *decoder = SkImageDecoder::Factory(&memoryStream); if (!decoder) { OutputDebugString(_T("SkImageDecoder::Factory failed\r\n")); FreeResource(hImgData); return NULL; } SkBitmap *bitmap = new SkBitmap(); if (SkImageDecoder::kSuccess == decoder->decode(&memoryStream, bitmap, kBGRA_8888_SkColorType, SkImageDecoder::kDecodePixels_Mode)) { FreeResource(hImgData); return bitmap; } //error OutputDebugString(_T("SkImageDecoder::decode failed\r\n")); FreeResource(hImgData); delete bitmap; return NULL; } ~~~ 使用非常简单: ~~~ SkBitmap *bitmap = loadImageFromResource(IDB_CLEAR, _T("PNG")); ~~~ 注意我添加的是png文件,rc文件内有类似这样的语句: ~~~ IDB_CLEAR PNG "images\\icon-clear.png" ~~~ 其中第二列是资源类型,调用loadImageFromResource时指定的类型与这里一致即可。 其他参考文章: - [**CEF Windows开发环境搭建**](http://blog.csdn.net/foruok/article/details/50468642) - [**CEF加载PPAPI插件**](http://blog.csdn.net/foruok/article/details/50485448) - [**VS2013编译最简单的PPAPI插件**](http://blog.csdn.net/foruok/article/details/50485461) - [**理解PPAPI的设计**](http://blog.csdn.net/foruok/article/details/50486788) - [**PPAPI插件与浏览器的交互过程**](http://blog.csdn.net/foruok/article/details/50494061) - [**Windows下从源码编译CEF**](http://blog.csdn.net/foruok/article/details/50498740) - [**编译PPAPI的media_stream_video示例**](http://blog.csdn.net/foruok/article/details/50498873) - [**PPAPI插件的绘图与输入事件处理**](http://blog.csdn.net/foruok/article/details/50499813) - [**在PPAPI插件中创建本地窗口**](http://blog.csdn.net/foruok/article/details/50513228) - [**PPAPI插件与浏览器的通信**](http://blog.csdn.net/foruok/article/details/50513315) - [**Windows下从源码编译Skia**](http://blog.csdn.net/foruok/article/details/50524726) - [**在PPAPI插件中使用Skia绘图**](http://blog.csdn.net/foruok/article/details/50526110)
';