1、IPicture接口对象的创建
方法1:直接通过文件创建
LPCSTR szFileUrl;
IPicture *pIPicture;
OleLoadPicturePath(CComBSTR(szFileUrl),
(LPUNKNOWN)NULL,
0,
0,
IID_IPicture,
(LPVOID*)&pIPicture))
方法2:通过IStream来创建
// 加载电子地图
LPCSTR szFileUrl;
IStream *pStream = NULL;
IPicture *pIPicture = NULL;;
CFileStatus fstatus;
CFile file;
LONG lFileSize;
HGLOBAL hGlobal;
if(file.Open(szFileUrl, CFile::modeRead) && file.GetStatus(szFileUrl, fstatus)
&& ((lFileSize = fstatus.m_size) != -1))
{
hGlobal = GlobalAlloc(GMEM_MOVEABLE, lFileSize);// 开辟大内存
if(hGlobal != NULL)
{
LPVOID pvData = NULL;
pvData = GlobalLock(hGlobal);
if(pvData != NULL)
{
file.ReadHuge(pvData, lFileSize); // 将图片数据保存在内存中
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &m_pIStream); // 创建流
file.Close();
}
else
{
GlobalFree(hGlobal);
return;
}
}
else
{
file.Close();
return;
}
}
else
{
//打开图片失败
return;
}
OleLoadPicture(m_pIStream, fstatus.m_size, TRUE, IID_IPicture, (LPVOID*)&m_pIPicture);
GlobalFree(hGlobal);
2、通过IPicture来获取图片的尺寸
OLE_XSIZE_HIMETRIC hmPicWidth;
OLE_YSIZE_HIMETRIC hmPicHeight;
pIPicture->get_Width(&hmPicWidth); // 获取图像宽度
pIPicture->get_Height(&hmPicHeight); // 获取图像高度
// 转化单位为象素
nPicWidth = MulDiv(hmPicWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX), HIMETRIC_PER_INCH);
nPicHeight = MulDiv(hmPicHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), HIMETRIC_PER_INCH);
3、通过IPicture来画图
void ShowPicture(CDC *pDC)
{
OLE_XPOS_HIMETRIC xSrc; // 图片中当前显示区域的x
OLE_YPOS_HIMETRIC ySrc; // 图片中当前显示区域的y
OLE_XSIZE_HIMETRIC cxSrc; // 图片中当前显示区域的宽度
OLE_YSIZE_HIMETRIC cySrc; // 图片中当前显示区域的高度
m_pIPicture->Render(*pDC, 0, 0, m_rcBox.Width(), m_rcBox.Height(), xSrc, ySrc, cxSrc, cySrc, NULL);
}
4、将IPicture中的图片保存起来
BOOL SaveToFileFromIPicture(LPCSTR szFileName, IPicture *pPic)
{
// Create ILockBytes Buffer
ILockBytes *plkbyt = NULL;
CreateILockBytesOnHGlobal(NULL, TRUE, &plkbyt);
// Create IStorage
IStorage *pStorage = NULL;
HRESULT hr = ::StgCreateDocfileOnILockBytes(plkbyt,
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage);
if (FAILED(hr))
{
plkbyt->Release();
plkbyt = NULL;
return FALSE;
}
// Create IStream
IStream *pStream = NULL;
hr = pStorage->CreateStream(L"PICTURE",
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, 0, &pStream);
if (FAILED(hr))
{
pStorage->Release();
pStorage = NULL;
plkbyt->Release();
plkbyt = NULL;
return FALSE;
}
// Copy Data Stream
long lSize;
hr = pPic->SaveAsFile(pStream, TRUE, &lSize);
if (FAILED(hr))
{
pStream->Release();
pStream = NULL;
pStorage->Release();
pStorage = NULL;
plkbyt->Release();
plkbyt = NULL;
return FALSE;
}
// Get Statistics For Final Size Of Byte Array
STATSTG statStg;
hr = plkbyt->Stat(&statStg, STATFLAG_NONAME);
if (FAILED(hr))
{
pStream->Release();
pStream = NULL;
pStorage->Release();
pStorage = NULL;
plkbyt->Release();
plkbyt = NULL;
return FALSE;
}
// calculate "Pure" Picture Data, Must Be In a 512 Blocks...
double dbSkipFloat = (double(lSize) / 512);
DWORD dwPicDataSize = 0;
if(dbSkipFloat > DWORD(dbSkipFloat))
{
dwPicDataSize = (DWORD)dbSkipFloat + 1;
}
else
{
dwPicDataSize = (DWORD)dbSkipFloat;
}
dwPicDataSize = dwPicDataSize * 512;
// Allocate Only The "Pure" Picture Data
BYTE *pPicDataBuffer = (BYTE*)malloc(dwPicDataSize);
if (pPicDataBuffer == NULL)
{
pStream->Release();
pStream = NULL;
plkbyt->Release();
pStorage->Release();
pStorage = NULL;
plkbyt = NULL;
return FALSE;
}
// Read "Pure" Picture Data to Buffer
_ULARGE_INTEGER ulOffset;
ulOffset.LowPart = 0;
ulOffset.HighPart = 0;
ulOffset.QuadPart = (DWORD)(statStg.cbSize.QuadPart - dwPicDataSize);
DWORD dwRealDataSize;
hr = plkbyt->ReadAt(ulOffset, pPicDataBuffer, dwPicDataSize, &dwRealDataSize);
if (FAILED(hr))
{
free(pPicDataBuffer);
pPicDataBuffer = NULL;
pStream->Release();
pStream = NULL;
pStorage->Release();
pStorage = NULL;
plkbyt->Release();
plkbyt = NULL;
return FALSE;
}
// Save "Pure" Picture Data to file
CFile fBmp;
CFileException e;
if (!fBmp.Open(szFileName, CFile::typeBinary | CFile::modeCreate | CFile::modeWrite, &e))
{
free(pPicDataBuffer);
pPicDataBuffer = NULL;
pStream->Release();
pStream = NULL;
pStorage->Release();
pStorage = NULL;
plkbyt->Release();
plkbyt = NULL;
return FALSE;
}
fBmp.Write(pPicDataBuffer, dwRealDataSize);
fBmp.Close();
free(pPicDataBuffer);
pPicDataBuffer = NULL;
pStream->Release();
pStream = NULL;
pStorage->Release();
pStorage = NULL;
plkbyt->Release();
plkbyt = NULL;
return TRUE;
}
5、将IPicture中图片按规定的尺寸保存
BOOL CIPictureDlg::Save2File(LPCSTR szFileName, IPicture *pPic, int nWidth, int nHeight)
{
// create a new IPicture
OLE_HANDLE hPic = NULL;
if (FAILED(pPic->get_Handle(&hPic)))
{
return FALSE;
}
HBITMAP hBmp = (HBITMAP)CopyImage((HANDLE)hPic,
IMAGE_BITMAP,
nWidth,
nWidth,
LR_CREATEDIBSECTION);
if (hBmp == NULL)
{
return FALSE;
}
PICTDESC picDesc;
picDesc.cbSizeofstruct = sizeof(PICTDESC);
picDesc.picType = PICTYPE_BITMAP;
picDesc.bmp.hbitmap = hBmp;
IPicture *pNewPic = NULL;
if (SUCCEEDED(OleCreatePictureIndirect(&picDesc, IID_IPicture, FALSE, (LPVOID *)&pNewPic)))
{
// Save to file
Save2File(szFileName, pNewPic);
pNewPic->Release();
pNewPic = NULL;
DeleteObject(hBmp);
hBmp = NULL;
return TRUE;
}
DeleteObject(hBmp);
hBmp = NULL;
return FALSE;
}
方法 | 描述 |
get_Handle | 返回图像对象的Windows GDI句柄 |
get_Hpal | 返回图像对象当前使用的调色板拷贝 |
get_Type | 返回当前图像对象的的图像类型 |
get_Width | 返回当前图像对象的图像宽度 |
get_Height | 返回当前图像对象的图像高度 |
Render | 在指定的位置、指定的设备上下文上绘制指定的图像部分 |
set_Hpal | 设置当前图像的调色板 |
get_CurDC | 返回当前选中这个图像的设备上下文 |
SelectPicture | 将一个位图图像选入给定的设备上下文,返回选中图像的设备上下文和图像的GDI句柄 |
get_KeepOriginalForma | 返回图像对象KeepOriginalFormat 属性的当前值 |
put_KeepOriginalFormat | 设置图像对象的KeepOriginalFormat 属性 |
PictureChanged | 通知图像对象它的图像资源改变了 |
SaveAsFile | 将图像数据存储到流中,格式与存成文件格式相同 |
get_Attributes | 返回图像位属性当前的设置 |
IPicture接口管理一个图片对象和它的属性。图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持。图像对象的主要接口是IPicture和IPictureDisp。IPictureDisp从IDispatch继承,提供了通过自动化访问图片属性的能力。图片对象可通过OleCreatePictureIndirect创建。关于IPicture支持的其他接口和方法可以看MSDN,一般创建图片对象可以用OleLoadPicture函数,它简化了基于流内容创建图片对象。
IPicture接口管理一个图片对象和它的属性。图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持。图像对象的主要接口是IPicture和IPictureDisp。IPictureDisp从IDispatch继承,提供了通过自动化访问图片属性的能力。图片对象可通过OleCreatePictureIndirect创建。关于IPicture支持的其他接口和方法可以看MSDN,一般创建图片对象可以用OleLoadPicture函数,它简化了基于流内容创建图片对象。下面的代码中有两个未定义的变量是FilePath和hDC。
//FilePath是从外部传入的图片路径
//打开文件
HANDLE hFile = CreateFile(FilePath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
_ASSERTE(INVALID_HANDLE_VALUE != hFile);
//取文件大小
DWORD dwFileSize = GetFileSize(hFile, NULL);
_ASSERTE(-1 != dwFileSize);
LPVOID pvData = NULL;
//分配内存,准备读入图片文件的数据
//GlobalAlloc从堆分配指定字节的内存区域
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
_ASSERTE(NULL != hGlobal);
//GlobalLock函数锁住一个全局的内存对象同时返回一个指向对象首字节的指针
pvData = GlobalLock(hGlobal);
_ASSERTE(NULL != pvData);
DWORD dwBytesRead = 0;
//读取文件的数据到分配的全局内存
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
_ASSERTE(FALSE != bRead);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
//到此,我们已经把文件的数据读到了内存当中
LPSTREAM pstm = NULL;
//从全局内存创建IStream接口指针
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
_ASSERTE(SUCCEEDED(hr) && pstm);
//根据图片文件创建IPicture接口指针
hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
_ASSERTE(SUCCEEDED(hr) && gpPicture);
pstm->Release();
//至此,IPicture接口建立好,下面开始画图片
//hDC是外部传入的画图设备
long hmWidth;
long hmHeight;
gpPicture->get_Width(&hmWidth);
gpPicture->get_Height(&hmHeight);
//转换himetric距离为pixels距离,1英寸=25.4毫米
int nWidth = MulDiv(hmWidth, GetDeviceCaps(hDC, LOGPIXELSX), 2540);
int nHeight = MulDiv(hmHeight, GetDeviceCaps(hDC, LOGPIXELSY), 2540);
RECT rc;
GetClientRect(hWnd, &rc);
//IPicture::Render显示图片
gpPicture->Render(hDC, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);