GDI+初始化bitmap对象
Bitmapbitmap_origin(L"aaa.bmp");constintHEIGHT=bitmap_origin.GetHeight();constintWIDT...
Bitmap bitmap_origin(L"aaa.bmp");
const int HEIGHT=bitmap_origin.GetHeight();
const int WIDTH=bitmap_origin.GetWidth();
获得的高和宽都为零,是不是因为没有初始化Bitmap对象?应该怎么弄? 展开
const int HEIGHT=bitmap_origin.GetHeight();
const int WIDTH=bitmap_origin.GetWidth();
获得的高和宽都为零,是不是因为没有初始化Bitmap对象?应该怎么弄? 展开
展开全部
你这个都没指定图片的路径。这里有个装载图片的例子,指定路径及图片的后缀名即可。
/////////////////////////////////////////////////////////////////////
// Defined class for GDI Plus Image.
class CImageHelper
{
// Constructor and destructor.
public:
CImageHelper() : m_pImage(NULL), m_pBitmap(NULL)
{
}
~CImageHelper()
{
DeleteOldImage();
}
// Data members.
public:
Image* m_pImage;
Bitmap* m_pBitmap;
// Operations.
private:
void DeleteOldImage()
{
if (m_pBitmap)
{
delete m_pBitmap;
m_pBitmap = NULL;
m_pImage = NULL;
}
}
public:
// Load image from resource manager.
Image* Load_Image(ATL::_U_STRINGorID resource, ATL::_U_STRINGorID type = (UINT) 0)
{
DeleteOldImage();
// Load image from path.
if ( HIWORD(resource.m_lpstr) != NULL)
{
m_pImage = new Image(resource.m_lpstr);
if (m_pImage != NULL)
{
m_pBitmap = static_cast<Bitmap*>(m_pImage);
return m_pImage;
}
ATLASSERT(FALSE);
return NULL;
}
// Load PNG, JPG or JPEG...
else if (type.m_lpstr != NULL && type.m_lpstr != RT_BITMAP)
{
CResource res;
if( !res.Load(type, resource) ) return NULL;
DWORD dwSize = res.GetSize();
HANDLE hMemory = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
if( hMemory == NULL ) return NULL;
// Copy resource to memory.
memcpy(GlobalLock(hMemory), res.Lock(), dwSize);
GlobalUnlock(hMemory);
// Create stream that write to image.
IStream* pStream = NULL;
CreateStreamOnHGlobal(hMemory, TRUE, &pStream);
m_pBitmap = new Bitmap(pStream);
m_pImage = static_cast<Image*>(m_pBitmap);
pStream->Release();
}
else
{
// This only loads BMP resources
m_pBitmap = new Gdiplus::Bitmap(_Module.GetResourceInstance(), (LPCWSTR) (UINT) resource.m_lpstr);
m_pImage = static_cast<Image*>(m_pBitmap);
}
return m_pImage;
}
inline float GetWidth()
{
return (float)m_pImage->GetWidth();
}
inline float GetHeight()
{
return (float)m_pImage->GetHeight();
}
};
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询