从资源中加载jpg, png到GDI+ Image, bmp InvalidParameter问题
来源:互联网 发布:查询mysql数据库密码 编辑:程序博客网 时间:2024/06/05 00:07
从资源中加载jpg, png到GDI+ Image, bmp InvalidParameter问题
从资源中加载jpg和png文件, 貌似不应该是个大问题, 一google结果一大堆, 却有两个陷阱,trap啊1, 是Bitmap(RT_BITMAP)类型的图片无法加载, RT_BITMAP是预定义类型, 资源里面没有bmp文件的头,SizeofResource 的返回值要比图片文件少几个字节,因为少了这几个字节, 所以GDI+会返回invalid parameter错误。
2, 从IStream里面创建出来的Image对象似乎会引用到堆里面的内存, 如果hBuffer被释放了, 创建的Image的内容就会被破坏,有时只能画出一小部分图片, 有时整个图片就是空白, 视当时的内存状况而定。调用Image的Clone也没用。
CYourClass::~CYourClass()
{
for(IMG_VECTOR::iterator it = m_arImage.begin(); it != m_arImage.end(); it++)
delete *it;
for(HGLB_VECTOR::iterator it = m_arGlobal.begin(); it != m_arGlobal.end(); it++)
{
::GlobalUnlock(*it);
::GlobalFree(*it);
}
}
void CYourClass::AddImage(HMODULE hInst, UINT nResourceID, LPCTSTR lpType)
{
if(lpType == RT_BITMAP)
{
//GDI+ can not load RT_BITMAP resouce,
//because they are predefined resource,
//they don't contains the image file header.
assert(FALSE);
return;
}
HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(nResourceID), lpType);
if (!hResource)
return;
DWORD imageSize = ::SizeofResource(hInst, hResource);
if (!imageSize)
return;
const void* pResourceData = ::LockResource(::LoadResource(hInst, hResource));
if (!pResourceData)
return;
HGLOBAL hBuffer = ::GlobalAlloc(GMEM_FIXED, imageSize);
if (NULL == hBuffer)
return;
void* pBuffer = ::GlobalLock(hBuffer);
if (pBuffer)
{
CopyMemory(pBuffer, pResourceData, imageSize);
IStream* pStream = NULL;
if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream) == S_OK)
{
Gdiplus::Image * pImage = Gdiplus::Image::FromStream(pStream);
pStream->Release();
if (pImage)
{
if (pImage->GetLastStatus() == Gdiplus::Ok &&
pImage->GetWidth() > 0)
{
m_arImage.push_back(pImage);
//it seems the image will take usage of the global memory.
//so the global memory should be kept until the image destroy.
m_arGlobal.push_back(hBuffer);
return;
}
delete pImage;
}
}
::GlobalUnlock(hBuffer);
}
::GlobalFree(hBuffer);
}
{
for(IMG_VECTOR::iterator it = m_arImage.begin(); it != m_arImage.end(); it++)
delete *it;
for(HGLB_VECTOR::iterator it = m_arGlobal.begin(); it != m_arGlobal.end(); it++)
{
::GlobalUnlock(*it);
::GlobalFree(*it);
}
}
void CYourClass::AddImage(HMODULE hInst, UINT nResourceID, LPCTSTR lpType)
{
if(lpType == RT_BITMAP)
{
//GDI+ can not load RT_BITMAP resouce,
//because they are predefined resource,
//they don't contains the image file header.
assert(FALSE);
return;
}
HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(nResourceID), lpType);
if (!hResource)
return;
DWORD imageSize = ::SizeofResource(hInst, hResource);
if (!imageSize)
return;
const void* pResourceData = ::LockResource(::LoadResource(hInst, hResource));
if (!pResourceData)
return;
HGLOBAL hBuffer = ::GlobalAlloc(GMEM_FIXED, imageSize);
if (NULL == hBuffer)
return;
void* pBuffer = ::GlobalLock(hBuffer);
if (pBuffer)
{
CopyMemory(pBuffer, pResourceData, imageSize);
IStream* pStream = NULL;
if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream) == S_OK)
{
Gdiplus::Image * pImage = Gdiplus::Image::FromStream(pStream);
pStream->Release();
if (pImage)
{
if (pImage->GetLastStatus() == Gdiplus::Ok &&
pImage->GetWidth() > 0)
{
m_arImage.push_back(pImage);
//it seems the image will take usage of the global memory.
//so the global memory should be kept until the image destroy.
m_arGlobal.push_back(hBuffer);
return;
}
delete pImage;
}
}
::GlobalUnlock(hBuffer);
}
::GlobalFree(hBuffer);
}
原地址:http://www.cppblog.com/aqazero/archive/2009/11/05/100221.html
- 从资源中加载jpg, png到GDI+ Image, bmp InvalidParameter问题
- 从资源中加载jpg, png到GDI+ Image
- 从资源中加载jpg, png到GDI+ Image
- GDI+实例教程之: 转换Image(BMP,JPG,GIF,PNG等) 到MetaFile(EMF等) 格式
- delphi 中 image 控件加载bmp、JPG、GIF、PNG等图片的办法
- delphi 中 image 控件加载bmp、JPG、GIF、PNG等图片的办法
- GDI+从资源中的加载PNG图片
- 使用GDI +加载JPG和PNG资源的CGdiPlusBitmap类
- VC.NET从资源中加载图片(jpg,png)并显示
- 加载多种图片格式(gif,jpg,png.bmp)
- VC6 CImage 加载jpg png bmp
- VC6 CImage 加载jpg png bmp gif
- GDI+从资源中加载图片
- GDI+从资源文件中加载图片
- GDI+ BMP转JPG
- 如何加载显示jpg ,png bmp等图片,利用CImage
- 资源中加载Png
- 将JPG,BMP,PNG图片插入到ACCESS数据库
- 自定义摄像机应用实现之预览界面展示
- Linux 内核信号表
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- poj 2935 Basic Wall Maze bfs
- tomcat 配置连接池
- 从资源中加载jpg, png到GDI+ Image, bmp InvalidParameter问题
- android_launcher的源码详细分析 .
- 用matlab(或者 opencv)标定出来的结果为什么焦距有两个值
- busybox: addusr unknown uid 0
- FLASH处理图像的移动、缩放、旋转、颜色变换的类推荐。
- eclipse导入曾经删除过的项目时 提示工作空间内该项目仍然存在
- Windows下多进程的实现案例
- KEIL 相关网址
- 11级_Java_曹建波 11.27 编写自己的JDBC框架