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); }
'개발언어 > c++' 카테고리의 다른 글
ATL Error 정보 얻기 (0) | 2016.10.19 |
---|---|
CFileDialog 사용법 (0) | 2016.10.19 |
MFC IP얻기 (1) | 2016.10.03 |
고속연속평균-실시간평균 (0) | 2016.09.16 |
SAFEARRAY를 사용한 VARIANT ARRAY(배열) 다루기(VC++) (0) | 2016.09.02 |