123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- #include "stdafx.h"
- #include "Picture.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define HIMETRIC_INCH 2540
- #define ERROR_TITLE "CPicture Error"
- CPicture::CPicture()
- {
- m_IPicture = NULL;
- m_Height = 0;
- m_Weight = 0;
- m_Width = 0;
- }
- CPicture::~CPicture()
- {
- if(m_IPicture != NULL) FreePictureData();
- }
- void CPicture::FreePictureData()
- {
- if(m_IPicture != NULL)
- {
- m_IPicture->Release();
- m_IPicture = NULL;
- m_Height = 0;
- m_Weight = 0;
- m_Width = 0;
- }
- }
- BOOL CPicture::Load(UINT ResourceName, LPCSTR ResourceType)
- {
- BOOL bResult = FALSE;
- HGLOBAL hGlobal = NULL;
- HRSRC hSource = NULL;
- LPVOID lpVoid = NULL;
- int nSize = 0;
- if(m_IPicture != NULL) FreePictureData();
- hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(ResourceName), ResourceType);
- if(hSource == NULL)
- {
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "FindResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- return(FALSE);
- }
- hGlobal = LoadResource(AfxGetResourceHandle(), hSource);
- if(hGlobal == NULL)
- {
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "LoadResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- return(FALSE);
- }
- lpVoid = LockResource(hGlobal);
- if(lpVoid == NULL)
- {
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "LockResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- return(FALSE);
- }
- nSize = (UINT)SizeofResource(AfxGetResourceHandle(), hSource);
- if(LoadPictureData((BYTE*)hGlobal, nSize)) bResult = TRUE;
- UnlockResource(hGlobal);
- FreeResource(hGlobal);
- m_Weight = nSize;
- if(m_IPicture != NULL)
- {
- m_IPicture->get_Height(&m_Height);
- m_IPicture->get_Width(&m_Width);
-
- 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);
- }
- BOOL CPicture::Load(CString sFilePathName)
- {
- BOOL bResult = FALSE;
- CFile PictureFile;
- CFileException e;
- int nSize = 0;
- if(m_IPicture != NULL) FreePictureData();
- 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
- {
- 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);
-
- 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);
- }
- 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
- {
- pStream->Release();
- pStream = NULL;
- bResult = TRUE;
- }
- }
- FreeResource(hGlobal);
- 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,
- DrawRect.top,
- DrawRect.right - DrawRect.left,
- DrawRect.bottom - DrawRect.top,
- 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);
- }
- BOOL CPicture::Show(CDC *pDC, CPoint LeftTop, CPoint WidthHeight, int MagnifyX, int MagnifyY)
- {
- if (pDC == NULL || m_IPicture == NULL) return FALSE;
-
- long Width = 0;
- long Height = 0;
- m_IPicture->get_Width(&Width);
- m_IPicture->get_Height(&Height);
- if(MagnifyX == NULL) MagnifyX = 0;
- if(MagnifyY == NULL) MagnifyY = 0;
- MagnifyX = int(MulDiv(Width, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);
- MagnifyY = int(MulDiv(Height,pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);
- CRect DrawRect(LeftTop.x, LeftTop.y, MagnifyX, MagnifyY);
- HRESULT hrP = NULL;
- hrP = m_IPicture->Render(pDC->m_hDC,
- LeftTop.x,
- LeftTop.y,
- WidthHeight.x +MagnifyX,
- WidthHeight.y +MagnifyY,
- 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);
- }
- BOOL CPicture::SaveAsBitmap(CString sFilePathName)
- {
- BOOL bResult = FALSE;
- ILockBytes *Buffer = 0;
- IStorage *pStorage = 0;
- IStream *FileStream = 0;
- BYTE *BufferBytes;
- STATSTG BytesStatistics;
- DWORD OutData;
- long OutStream;
- CFile BitmapFile; CFileException e;
- double SkipFloat = 0;
- DWORD ByteSkip = 0;
- _ULARGE_INTEGER RealData;
- CreateILockBytesOnHGlobal(NULL, TRUE, &Buffer);
- HRESULT hr = ::StgCreateDocfileOnILockBytes(Buffer,
- STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage);
- hr = pStorage->CreateStream(L"PICTURE",
- STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, 0, &FileStream);
- m_IPicture->SaveAsFile(FileStream, TRUE, &OutStream);
- FileStream->Release();
- pStorage->Release();
- Buffer->Flush();
-
- Buffer->Stat(&BytesStatistics, STATFLAG_NONAME);
-
- SkipFloat = (double(OutStream) / 512);
- if(SkipFloat > DWORD(SkipFloat)) ByteSkip = (DWORD)SkipFloat + 1;
- else ByteSkip = (DWORD)SkipFloat;
- ByteSkip = ByteSkip * 512;
-
-
- ByteSkip = (DWORD)(BytesStatistics.cbSize.QuadPart - ByteSkip);
-
- RealData.LowPart = 0;
- RealData.HighPart = 0;
- RealData.QuadPart = ByteSkip;
- BufferBytes = (BYTE*)malloc(OutStream);
- if(BufferBytes == NULL)
- {
- Buffer->Release();
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- }
- Buffer->ReadAt(RealData, BufferBytes, OutStream, &OutData);
- if(BitmapFile.Open(sFilePathName, CFile::typeBinary | CFile::modeCreate | CFile::modeWrite, &e))
- {
- BitmapFile.Write(BufferBytes, OutData);
- BitmapFile.Close();
- bResult = TRUE;
- }
- else
- {
- 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;
- }
-
- Buffer->Release();
- free(BufferBytes);
- return(bResult);
- }
- BOOL CPicture::ShowBitmapResource(CDC *pDC, const int BMPResource, CPoint LeftTop)
- {
- if (pDC == NULL) return(FALSE);
- CBitmap BMP;
- if(BMP.LoadBitmap(BMPResource))
- {
-
- BITMAP BMPInfo;
- BMP.GetBitmap(&BMPInfo);
-
- CDC DCMemory;
- DCMemory.CreateCompatibleDC(pDC);
-
- CBitmap* pOldBitmap = DCMemory.SelectObject(&BMP);
-
- pDC->BitBlt(LeftTop.x, LeftTop.y, BMPInfo.bmWidth, BMPInfo.bmHeight, &DCMemory, 0, 0, SRCCOPY);
- DCMemory.SelectObject(pOldBitmap);
- }
- else
- {
- TRACE0("ERROR: Can Not Find The Bitmap Resource\n");
- return(FALSE);
- }
- return(TRUE);
- }
- BOOL CPicture::UpdateSizeOnDC(CDC *pDC)
- {
- if(pDC == NULL || m_IPicture == NULL) { m_Height = 0; m_Width = 0; return(FALSE); };
- m_IPicture->get_Height(&m_Height);
- m_IPicture->get_Width(&m_Width);
-
- int CurrentDPI_X = pDC->GetDeviceCaps(LOGPIXELSX);
- int CurrentDPI_Y = pDC->GetDeviceCaps(LOGPIXELSY);
-
- if(pDC->IsPrinting())
- {
- CurrentDPI_X = 96;
- CurrentDPI_Y = 96;
- }
- m_Height = MulDiv(m_Height, CurrentDPI_Y, HIMETRIC_INCH);
- m_Width = MulDiv(m_Width, CurrentDPI_X, HIMETRIC_INCH);
- return(TRUE);
- }
|