JPG, GIF, BMP, WMF, EMF, ICO 열기(IPicture 사용법)

MFC 에서 JPG나 GIF 등의 이미지를 불러올 수 가 없다. 정확히는 방법을 찾기가 너무 어렵다.
IPicture 로  그림파일 불러오는 방법이 있습니다.

파일 읽기

BOOL CPicture::Load(CString sFilePathName)ImageLoad.zip
{
BOOL bResult = FALSE;
CFile PictureFile;
CFileException e;
int nSize = 0;
 if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
 if(PictureFile.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e))
{
nSize = PictureFile.GetLength();
BYTE* pBuffer = new BYTE[nSize];
if(PictureFile.Read(pBuffer, nSize) > 0)
{
if(LoadPictureData(pBuffer, nSize)) bResult = TRUE;
}
  PictureFile.Close();
delete [] pBuffer;
}
else // Open Failed...
{
TCHAR szCause[255];
e.GetErrorMessage(szCause, 255, NULL);
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, szCause, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
bResult = FALSE;
}
 m_Weight = nSize;
 if(m_IPicture != NULL)
{
m_IPicture->get_Height(&m_Height);
m_IPicture->get_Width(&m_Width);
//  "Standard" (96 DPI) Device Context
m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
m_Width  = MulDiv(m_Width,  96, HIMETRIC_INCH);
}
else
{
m_Height = 0;
m_Width = 0;
bResult = FALSE;
}
 return(bResult);
}

OleLoadPicture를 이용한 Data Laoding

BOOL CPicture::LoadPictureData(BYTE *pBuffer, int nSize)
{
BOOL bResult = FALSE;
 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
 if(hGlobal == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
 void* pData = GlobalLock(hGlobal);
memcpy(pData, pBuffer, nSize);
GlobalUnlock(hGlobal);
 IStream* pStream = NULL;
 if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
HRESULT hr;
if((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture)) == E_NOINTERFACE)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
else // S_OK
{
pStream->Release();
pStream = NULL;
bResult = TRUE;
}
}
 FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
 return(bResult);
}

이미지 출력

BOOL CPicture::Show(CDC *pDC, CRect DrawRect)
{
if (pDC == NULL || m_IPicture == NULL) return FALSE;
long Width  = 0;
long Height = 0;
m_IPicture->get_Width(&Width);
m_IPicture->get_Height(&Height);
    HRESULT hrP = NULL;
hrP = m_IPicture->Render(pDC->m_hDC,
DrawRect.left,                  // Left
DrawRect.top,                   // Top
DrawRect.right - DrawRect.left, // Right
DrawRect.bottom - DrawRect.top, // Bottom
0,
Height,
Width,
-Height,
&DrawRect);
    if (SUCCEEDED(hrP)) return(TRUE);
 HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}

“JPG, GIF, BMP, WMF, EMF, ICO 열기(IPicture 사용법)”의 12개의 댓글

  1. Excellent post. I was checking continuously this blog and I am impressed!
    Extremely useful information specially the last part 🙂 I care
    for such info much. I was looking for this particular info for
    a very long time. Thank you and good luck.

  2. Write more, thats all I have to say. Literally, it
    seems as though you relied on the video to make your point.
    You obviously know what youre talking about, why throw away
    your intelligence on just posting videos to your weblog when you could
    be giving us something informative to read?

댓글 남기기