现在我有图片的数据
BYTE *pbyImage;//保存jpg图片的二进制数据;
int nImagelen;//保存当前jpg图片的长度;
以前一直用的别人给的控件显示, 现在我想用现成的
控件显示,如picture 或 satic控件显示图片.
现在我有这个图片的数据和长度怎么用控件显示?
//picture控件变量
CStatic m_Pic;
BYTE *pbyImage;//保存jpg图片的二进制数据;
int nImagelen;//保存当前jpg图片的长度;
以前一直用的别人给的控件显示, 现在我想用现成的
控件显示,如picture 或 satic控件显示图片.
现在我有这个图片的数据和长度怎么用控件显示?
//picture控件变量
CStatic m_Pic;
这个是用图片路径显示,我的需求是用图片数据显示,不一样啊..
void CExportImageDlg::OnLvnItemchangedListDatabase(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
int nSel = pNMLV->iItem;
if (nSel < 0)
return;
BYTE *pbyImage = (BYTE*)m_DataList.GetItemData(nSel);
if (pbyImage)
{
int nImagelen = *(UINT*)pbyImage;
pbyImage += sizeof(UINT); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE,nImagelen);
if (hGlobal == NULL)
{
AfxMessageBox(L"GlobalAlloc error");
return;
}
BYTE *pbyData = (BYTE*)GlobalLock(hGlobal);
memcpy(pbyData, &pbyImage, nImagelen);
GlobalUnlock(hGlobal); LPSTREAM pstm = NULL;
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
if (hr != S_OK)
{
AfxMessageBox(L"CreateStreamOnHGlobal error");
return;
}
CImage Image;
HRESULT hLoad = Image.Load(pstm);
if ( hLoad != S_OK)
{ //GetLastError();获得的是 0
DWORD dwError = GetLastError();
CString strErrMsg;
strErrMsg.Format(_T("Load Error ,the error code is %d"), dwError); AfxMessageBox(strErrMsg);
return;
}
CRect rect;
m_Pic.GetWindowRect(&rect);
HDC hdc = ::GetDC(m_Pic.m_hWnd);
if (hdc)
{
Image.Draw(hdc, 0, 0);
}
} *pResult = 0;
}
void CExportImageDlg::OnLvnItemchangedListDatabase(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
int nSel = pNMLV->iItem;
if (nSel < 0)
return;
BYTE *pbyImage = (BYTE*)m_DataList.GetItemData(nSel);
if (pbyImage)
{
int nImagelen = *(UINT*)pbyImage;
pbyImage += sizeof(UINT); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE,nImagelen);
if (hGlobal == NULL)
{
AfxMessageBox(L"GlobalAlloc error");
return;
}
BYTE *pbyData = (BYTE*)GlobalLock(hGlobal);
memcpy(pbyData, pbyImage, nImagelen);
GlobalUnlock(hGlobal); LPSTREAM pstm = NULL;
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
if (hr != S_OK)
{
AfxMessageBox(L"CreateStreamOnHGlobal error");
return;
}
CImage Image;
Image.Destroy(); HRESULT hLoad = Image.Load(pstm);
if ( hLoad != S_OK)
{
DWORD dwError = GetLastError();
CString strErrMsg;
strErrMsg.Format(_T("Load Error ,the error code is %d"), dwError);
AfxMessageBox(strErrMsg);
return;
}
CRect rect;
m_Pic.GetWindowRect(&rect);
HDC hdc = ::GetDC(m_Pic.m_hWnd);
if (hdc)
{
Image.Draw(hdc, 0, 0, rect.Width(),rect.Height());
m_Pic.Invalidate();
}
}
*pResult = 0;
}