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)
- 前言
- CEF Windows開發環境搭建
- CEF加載PPAPI插件
- VS2013編譯最簡單的PPAPI插件
- 理解PPAPI的設計
- PPAPI插件與瀏覽器的交互過程
- Windows下從源碼編譯CEF
- 編譯PPAPI的media_stream_video示例
- PPAPI插件的繪圖與輸入事件處理
- 在PPAPI插件中創建本地窗口
- PPAPI插件與瀏覽器的通信
- Windows下從源碼編譯Skia
- 在PPAPI插件中使用Skia繪圖
- 加載DLL中的圖片資源生成Skia中的SkBitmap對象
- PPAPI+Skia實現的涂鴉板
- PPAPI中使用Chromium的3D圖形接口
- PPAPI中使用OpenGL ES繪圖