123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- // Picture.cpp: implementation of the CPicture class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "newclient.h"
- #include "Picture.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CPicture::CPicture()
- {
- }
- CPicture::~CPicture()
- {
- }
- //-----------------------------------------------------------------------------
- // Does: Free The Allocated Memory That Holdes The IPicture Interface Data
- // ~~~~ And Clear Picture Information
- //
- // Note: This Might Also Be Useful If U Only Need To Show The Picture Once
- // ~~~~~ Or If U Copy The Picture To The Device Context, So It Can Still
- // Remain On Screen - But IPicture Data Is Not Needed No More
- //
- //-----------------------------------------------------------------------------
- void CPicture::UnloadPicture()
- //=============================================================================
- {
- if(m_pPict != NULL){
- m_pPict->Release();
- m_pPict = NULL;
- }
- }
- //-----------------------------------------------------------------------------
- // Does: Open a Resource And Load It Into IPicture (Interface)
- // ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
- //
- // Note: When Adding a Bitmap Resource It Would Automatically Show On "Bitmap"
- // ~~~~ This NOT Good Coz We Need To Load It From a Custom Resource "BMP"
- // To Add a Custom Rresource: Import Resource -> Open As -> Custom
- // (Both .BMP And .DIB Should Be Found Under "BMP")
- //
- // InPut: ResourceName - As a UINT Defined (Example: IDR_PICTURE_RESOURCE)
- // ~~~~~ ResourceType - Type Name (Example: "JPG")
- //
- // OutPut: TRUE If Succeeded...
- // ~~~~~~
- //-----------------------------------------------------------------------------
- BOOL CPicture::Load(HINSTANCE hInstance,LPCTSTR lpszResourceName, LPCSTR ResourceType)
- //=============================================================================
- {
- HGLOBAL hGlobal = NULL;
- HRSRC hSource = NULL;
- LPVOID lpVoid = NULL;
- int nSize = 0;
- BOOL bResult=FALSE;
- if(m_pPict != NULL) UnloadPicture(); // Important - Avoid Leaks...
- hSource = FindResource(hInstance, lpszResourceName, ResourceType);
- if(hSource == NULL)
- {
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "FindResource() Failed\t", "ERROR"/*ERROR_TITLE*/, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- return(FALSE);
- }
- hGlobal = LoadResource(hInstance, hSource);
- if(hGlobal == NULL)
- {
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "LoadResource() Failed\t", "ERROR"/*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"/*ERROR_TITLE*/, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- return(FALSE);
- }
- nSize = (UINT)SizeofResource(hInstance, hSource);
- if(LoadPictureData((BYTE*)hGlobal, nSize)) bResult = TRUE;
- UnlockResource(hGlobal); // 16Bit Windows Needs This
- FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
- return(bResult);
- }
- //-----------------------------------------------------------------------------
- // Does: Open a File And Load It Into IPicture (Interface)
- // ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
- //
- // InPut: sFilePathName - Path And FileName Target To Save
- // ~~~~~
- //
- // OutPut: TRUE If Succeeded...
- // ~~~~~~
- //-----------------------------------------------------------------------------
- BOOL CPicture::Load(CString sFilePathName)
- //=============================================================================
- {
- //if(!PathFileExists(sFilePathName))return FALSE;
- /*
- CFile file;
- if (!file.Open(sFilePathName, CFile::modeRead/ *|CFile::shareDenyWrite* /))
- return FALSE;*/
-
-
- BOOL bResult = FALSE;
- CFile PictureFile;
- CFileException e;
- int nSize = 0;
- if(m_pPict != NULL) UnloadPicture(); // 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"/*ERROR_TITLE*/, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- bResult = FALSE;
- }
- return(bResult);
- }
- //-----------------------------------------------------------------------------
- // Does: Read The Picture Data From a Source (File / Resource)
- // ~~~~ And Load It Into The Current IPicture Object In Use
- //
- // InPut: Buffer Of Data Source (File / Resource) And Its Size
- // ~~~~~
- //
- // OutPut: Feed The IPicture Object With The Picture Data
- // ~~~~~~ (Use Draw Functions To Show It On a Device Context)
- // TRUE If Succeeded...
- //-----------------------------------------------------------------------------
- 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"/*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_pPict)) == E_NOINTERFACE)
- {
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, "IPicture interface is not supported\t", "ERROR"/*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);
- }
- //-----------------------------------------------------------------------------
- // Does: Draw The Loaded Picture Direct To The Client DC
- // ~~~~
- //
- // Note: Bigger OR Smaller Dimentions Than The Original Picture Size
- // ~~~~ Will Draw The Picture Streached To Its New Given NEW Dimentions...
- //
- // InPut: pDC - Given DC To Draw On
- // ~~~~~ pSrcRect- Dimentions Of The Picture To Draw From(As a Rectangle)
- // DrawRect - Dimentions Of The Picture To Draw To(As a Rectangle)
- // OutPut: TRUE If Succeeded...
- // ~~~~~~
- //-----------------------------------------------------------------------------
- //=============================================================================
- void CPicture::Render(CDC* pDC, LPRECT pDrawRect, LPRECT pSrcRect/*=NULL*/,LPCRECT prcWBounds/*=NULL*/)
- {
- if (pDC == NULL || m_pPict == NULL) return ;
-
- CRect recrDest(pDrawRect);
- long Width = 0;
- long Height = 0;
- m_pPict->get_Width(&Width);
- m_pPict->get_Height(&Height);
- CRect SrcRect(0,0,Width,Height);
- if(pSrcRect){
- SrcRect=*pSrcRect;
- }
- CRect DrawRect(pDrawRect);
- HRESULT hrP = NULL;
- hrP = m_pPict->Render(pDC->m_hDC,
- DrawRect.left, // Left
- DrawRect.top, // Top
- DrawRect.Width(), // Right
- DrawRect.Height(), // Bottom
- SrcRect.left,
- SrcRect.top,
- SrcRect.Width(),
- SrcRect.Height(),
- prcWBounds);
- if (SUCCEEDED(hrP)) return;
- AfxThrowMemoryException();
- return;
- }//-----------------------------------------------------------------------------
- // Does: Saves The Picture That Is Stored In The IPicture Object As a Bitmap
- // ~~~~ (Converts From Any Known Picture Type To a Bitmap / Icon File)
- //
- // InPut: sFilePathName - Path And FileName Target To Save
- // ~~~~~
- //
- // OutPut: TRUE If Succeeded...
- // ~~~~~~
- //-----------------------------------------------------------------------------
- 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); // Create ILockBytes 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_pPict->SaveAsFile(FileStream, TRUE, &OutStream); // Copy Data Stream
- FileStream->Release();
- pStorage->Release();
- Buffer->Flush();
- // Get Statistics For Final Size Of Byte Array
- Buffer->Stat(&BytesStatistics, STATFLAG_NONAME);
- // Cut UnNeeded Data Coming From SaveAsFile() (Leave Only "Pure" Picture Data)
- SkipFloat = (double(OutStream) / 512); // Must Be In a 512 Blocks...
- if(SkipFloat > DWORD(SkipFloat)) ByteSkip = (DWORD)SkipFloat + 1;
- else ByteSkip = (DWORD)SkipFloat;
- ByteSkip = ByteSkip * 512; // Must Be In a 512 Blocks...
-
- // Find Difference Between The Two Values
- ByteSkip = (DWORD)(BytesStatistics.cbSize.QuadPart - ByteSkip);
- // Allocate Only The "Pure" Picture Data
- 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"/*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 // Write File Failed...
- {
- TCHAR szCause[255];
- e.GetErrorMessage(szCause, 255, NULL);
- HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
- MessageBoxEx(hWnd, szCause, "ERROR"/*ERROR_TITLE*/, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
- bResult = FALSE;
- }
-
- Buffer->Release();
- free(BufferBytes);
- return(bResult);
- }
- LONG CPicture::get_Height()
- {
- LONG nHeight = 0;
- if (m_pPict != NULL)
- {
- m_pPict->get_Height(&nHeight);
- }
- return nHeight;
- }
- LONG CPicture::get_Width()
- {
- LONG nWidth = 0;
- if (m_pPict != NULL)
- {
- m_pPict->get_Width(&nWidth);
- }
- return nWidth;
- }
|