Picture.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. //-----------------------------------------------------------------------------
  2. // Picture (Implementations) Version 1.00
  3. //
  4. // Routins 4 Showing Picture Files... (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
  5. //
  6. // Author: Dr. Yovav Gad, EMail: Sources@SuperMain.com ,Web: www.SuperMain.com
  7. //=============================================================================
  8. //
  9. // Full Story:
  10. // ~~~~~~~~~~~
  11. // There R Many Libraries To Handle Image Files, Anyway Most Of Them Do Not
  12. // Include Source Files Or Just Very Complicated To Implement / Understand,
  13. //
  14. // After Many Days Of Searching (And Not Finding) a Way To Load a JPG From a
  15. // Resource And Show It On a *Dialog Based* Application, I Decided 2 Take Steps
  16. //
  17. // So I Created What I Call a Very *Simple* & Useful Class,
  18. // It Can Easily Implemented By Adding It To a Project, And U Do Not Have To
  19. // Be a Real JPEG Freak - And Invent All Header Reading From The Beginning
  20. // (It Uses The IPicture Interface - Same Way As Internet Explorer Does)
  21. //
  22. // I Would Like To Thank Mr.Peter Hendrix For His Wonderful Work
  23. // That I Found On: http://www.thecodeproject.com/bitmap/cpicture.asp
  24. // Which I Was Inspired And Got The IPicture Interface Idea From...
  25. //
  26. // Guess U Will Find It Useful,
  27. // Appreciate If U Can Mention My Name On Your Final Code,
  28. // Please Feel Free To Send Me Any Improvements Or SaveAsJPG() Functions:
  29. //
  30. // (This Program Has No Bugs - Only Undocumented Solutions)
  31. //
  32. // Author: Dr. Yovav Gad, EMail: Sources@SuperMain.com ,Web: www.SuperMain.com
  33. //
  34. //--------------------------Example & Usage 4 Dummies--------------------------
  35. //
  36. // U Need 2 Add "CPicture.CPP" and "CPicture.H" Into Your Project (From FileView)
  37. // So U Will Get Control Over The Functions In This Class,
  38. // Then U Can Create a Picture Object And Show It On a Device Context
  39. //
  40. // CPicture m_Picture; // Create a Picture Object (An Instance Of This Class)
  41. // #include "Picture.h" // Make Sure U Include This Where U Gonna Create The Object...
  42. //
  43. // Load Picture Data Into The IPicture Interface (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
  44. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. // m_Picture.Load("Test.JPG"); // Load From a File - Just Load It (Show Later)
  46. // m_Picture.Load(IDR_TEST, "JPG"); // Load From a Resource - Just Load It (Show Later)
  47. // (U Must Include IDR_TEST In Your Resources Under a Custom Name, 4 Example - "JPG")
  48. //
  49. // When Using DC Object On a *Dialog Based* Application (CPaintDC dc(this);)
  50. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. // m_Picture.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels
  52. // m_Picture.Show(&dc, CPoint(0,0), CPoint(m_Picture.m_Width, m_Picture.m_Height), 0,0);
  53. // m_Picture.Show(&dc, CRect(0,0,100,100)); // Change Original Dimentions
  54. // m_Picture.ShowBitmapResource(&dc, IDB_TEST, CPoint(0,0)); // Show Bitmap Resource
  55. //
  56. // OR When Using a Pointer On a "Regular" MFC Application (CDC* pDC)
  57. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. // m_Picture.UpdateSizeOnDC(pDC); // Get Picture Dimentions In Pixels
  59. // m_Picture.Show(pDC, CPoint(0,0), CPoint(m_Picture.m_Width, m_Picture.m_Height), 0,0);
  60. // m_Picture.Show(pDC, CRect(0,0,100,100)); // Change Original Dimentions
  61. // m_Picture.ShowBitmapResource(pDC, IDB_TEST, CPoint(0,0)); // Show Bitmap Resource
  62. //
  63. // Show Picture Information
  64. // ~~~~~~~~~~~~~~~~~~~~~~~~
  65. // CString S;
  66. // S.Format("Size = %4d\nWidth = %4d\nHeight = %4d\nWeight = %4d\n",
  67. // m_Picture.m_Weight, m_Picture.m_Width, m_Picture.m_Height, m_Picture.m_Weight);
  68. // AfxMessageBox(S);
  69. //
  70. //----------------------------Cut The Bullshit Here----------------------------
  71. #include "stdafx.h"
  72. #include "Picture.h"
  73. #ifdef _DEBUG
  74. #define new DEBUG_NEW
  75. #undef THIS_FILE
  76. static char THIS_FILE[] = __FILE__;
  77. #endif
  78. #define HIMETRIC_INCH 2540
  79. #define ERROR_TITLE "CPicture Error" // Error Title (Related To This Class)...
  80. //-----------------------------------------------------------------------------
  81. // Does: Constructor - Create a New CPicture Object To Hold Picture Data
  82. // ~~~~
  83. //
  84. //-----------------------------------------------------------------------------
  85. CPicture::CPicture()
  86. //=============================================================================
  87. {
  88. m_IPicture = NULL;
  89. m_Height = 0;
  90. m_Weight = 0;
  91. m_Width = 0;
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Does: Destructor - Free Data And Information From The CPicture Object
  95. // ~~~~
  96. //
  97. //-----------------------------------------------------------------------------
  98. CPicture::~CPicture()
  99. //=============================================================================
  100. {
  101. if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Does: Free The Allocated Memory That Holdes The IPicture Interface Data
  105. // ~~~~ And Clear Picture Information
  106. //
  107. // Note: This Might Also Be Useful If U Only Need To Show The Picture Once
  108. // ~~~~~ Or If U Copy The Picture To The Device Context, So It Can Still
  109. // Remain On Screen - But IPicture Data Is Not Needed No More
  110. //
  111. //-----------------------------------------------------------------------------
  112. void CPicture::FreePictureData()
  113. //=============================================================================
  114. {
  115. if(m_IPicture != NULL)
  116. {
  117. m_IPicture->Release();
  118. m_IPicture = NULL;
  119. m_Height = 0;
  120. m_Weight = 0;
  121. m_Width = 0;
  122. }
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Does: Open a Resource And Load It Into IPicture (Interface)
  126. // ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
  127. //
  128. // Note: When Adding a Bitmap Resource It Would Automatically Show On "Bitmap"
  129. // ~~~~ This NOT Good Coz We Need To Load It From a Custom Resource "BMP"
  130. // To Add a Custom Rresource: Import Resource -> Open As -> Custom
  131. // (Both .BMP And .DIB Should Be Found Under "BMP")
  132. //
  133. // InPut: ResourceName - As a UINT Defined (Example: IDR_PICTURE_RESOURCE)
  134. // ~~~~~ ResourceType - Type Name (Example: "JPG")
  135. //
  136. // OutPut: TRUE If Succeeded...
  137. // ~~~~~~
  138. //-----------------------------------------------------------------------------
  139. BOOL CPicture::Load(UINT ResourceName, LPCSTR ResourceType)
  140. //=============================================================================
  141. {
  142. BOOL bResult = FALSE;
  143. HGLOBAL hGlobal = NULL;
  144. HRSRC hSource = NULL;
  145. LPVOID lpVoid = NULL;
  146. int nSize = 0;
  147. if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
  148. hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(ResourceName), ResourceType);
  149. if(hSource == NULL)
  150. {
  151. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  152. MessageBoxEx(hWnd, "FindResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  153. return(FALSE);
  154. }
  155. hGlobal = LoadResource(AfxGetResourceHandle(), hSource);
  156. if(hGlobal == NULL)
  157. {
  158. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  159. MessageBoxEx(hWnd, "LoadResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  160. return(FALSE);
  161. }
  162. lpVoid = LockResource(hGlobal);
  163. if(lpVoid == NULL)
  164. {
  165. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  166. MessageBoxEx(hWnd, "LockResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  167. return(FALSE);
  168. }
  169. nSize = (UINT)SizeofResource(AfxGetResourceHandle(), hSource);
  170. if(LoadPictureData((BYTE*)hGlobal, nSize)) bResult = TRUE;
  171. UnlockResource(hGlobal); // 16Bit Windows Needs This
  172. FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
  173. m_Weight = nSize; // Update Picture Size Info...
  174. if(m_IPicture != NULL) // Do Not Try To Read From Memory That Is Not Exist...
  175. {
  176. m_IPicture->get_Height(&m_Height);
  177. m_IPicture->get_Width(&m_Width);
  178. // Calculate Its Size On a "Standard" (96 DPI) Device Context
  179. m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
  180. m_Width = MulDiv(m_Width, 96, HIMETRIC_INCH);
  181. }
  182. else // Picture Data Is Not a Known Picture Type
  183. {
  184. m_Height = 0;
  185. m_Width = 0;
  186. bResult = FALSE;
  187. }
  188. return(bResult);
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Does: Open a File And Load It Into IPicture (Interface)
  192. // ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
  193. //
  194. // InPut: sFilePathName - Path And FileName Target To Save
  195. // ~~~~~
  196. //
  197. // OutPut: TRUE If Succeeded...
  198. // ~~~~~~
  199. //-----------------------------------------------------------------------------
  200. BOOL CPicture::Load(CString sFilePathName)
  201. //=============================================================================
  202. {
  203. BOOL bResult = FALSE;
  204. CFile PictureFile;
  205. CFileException e;
  206. int nSize = 0;
  207. if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
  208. if(PictureFile.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e))
  209. {
  210. nSize = PictureFile.GetLength();
  211. BYTE* pBuffer = new BYTE[nSize];
  212. if(PictureFile.Read(pBuffer, nSize) > 0)
  213. {
  214. if(LoadPictureData(pBuffer, nSize)) bResult = TRUE;
  215. }
  216. PictureFile.Close();
  217. delete [] pBuffer;
  218. }
  219. else // Open Failed...
  220. {
  221. TCHAR szCause[255];
  222. e.GetErrorMessage(szCause, 255, NULL);
  223. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  224. MessageBoxEx(hWnd, szCause, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  225. bResult = FALSE;
  226. }
  227. m_Weight = nSize; // Update Picture Size Info...
  228. if(m_IPicture != NULL) // Do Not Try To Read From Memory That Is Not Exist...
  229. {
  230. m_IPicture->get_Height(&m_Height);
  231. m_IPicture->get_Width(&m_Width);
  232. // Calculate Its Size On a "Standard" (96 DPI) Device Context
  233. m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
  234. m_Width = MulDiv(m_Width, 96, HIMETRIC_INCH);
  235. }
  236. else // Picture Data Is Not a Known Picture Type
  237. {
  238. m_Height = 0;
  239. m_Width = 0;
  240. bResult = FALSE;
  241. }
  242. return(bResult);
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Does: Read The Picture Data From a Source (File / Resource)
  246. // ~~~~ And Load It Into The Current IPicture Object In Use
  247. //
  248. // InPut: Buffer Of Data Source (File / Resource) And Its Size
  249. // ~~~~~
  250. //
  251. // OutPut: Feed The IPicture Object With The Picture Data
  252. // ~~~~~~ (Use Draw Functions To Show It On a Device Context)
  253. // TRUE If Succeeded...
  254. //-----------------------------------------------------------------------------
  255. BOOL CPicture::LoadPictureData(BYTE *pBuffer, int nSize)
  256. //=============================================================================
  257. {
  258. BOOL bResult = FALSE;
  259. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
  260. if(hGlobal == NULL)
  261. {
  262. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  263. MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  264. return(FALSE);
  265. }
  266. void* pData = GlobalLock(hGlobal);
  267. memcpy(pData, pBuffer, nSize);
  268. GlobalUnlock(hGlobal);
  269. IStream* pStream = NULL;
  270. if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
  271. {
  272. HRESULT hr;
  273. if((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture)) == E_NOINTERFACE)
  274. {
  275. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  276. MessageBoxEx(hWnd, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  277. return(FALSE);
  278. }
  279. else // S_OK
  280. {
  281. pStream->Release();
  282. pStream = NULL;
  283. bResult = TRUE;
  284. }
  285. }
  286. FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
  287. return(bResult);
  288. }
  289. //-----------------------------------------------------------------------------
  290. // Does: Draw The Loaded Picture Direct To The Client DC
  291. // ~~~~
  292. //
  293. // Note: Bigger OR Smaller Dimentions Than The Original Picture Size
  294. // ~~~~ Will Draw The Picture Streached To Its New Given NEW Dimentions...
  295. //
  296. // InPut: pDC - Given DC To Draw On
  297. // ~~~~~ DrawRect - Dimentions Of The Picture To Draw (As a Rectangle)
  298. //
  299. // OutPut: TRUE If Succeeded...
  300. // ~~~~~~
  301. //-----------------------------------------------------------------------------
  302. BOOL CPicture::Show(CDC *pDC, CRect DrawRect)
  303. //=============================================================================
  304. {
  305. if (pDC == NULL || m_IPicture == NULL) return FALSE;
  306. long Width = 0;
  307. long Height = 0;
  308. m_IPicture->get_Width(&Width);
  309. m_IPicture->get_Height(&Height);
  310. HRESULT hrP = NULL;
  311. hrP = m_IPicture->Render(pDC->m_hDC,
  312. DrawRect.left, // Left
  313. DrawRect.top, // Top
  314. DrawRect.right - DrawRect.left, // Right
  315. DrawRect.bottom - DrawRect.top, // Bottom
  316. 0,
  317. Height,
  318. Width,
  319. -Height,
  320. &DrawRect);
  321. if (SUCCEEDED(hrP)) return(TRUE);
  322. #if 0
  323. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  324. MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  325. #endif
  326. return(FALSE);
  327. }
  328. //-----------------------------------------------------------------------------
  329. // Does: Draw The Loaded Picture Direct To The Client DC
  330. // ~~~~
  331. //
  332. // Note: Bigger OR Smaller Dimentions Than The Original Picture Size
  333. // ~~~~ Will Draw The Picture Streached To Its New Given Dimentions...
  334. //
  335. // InPut: pDC - Given DC To Draw On
  336. // ~~~~~ LeftTop - Opening Point To Start Drawing (Left,Top)
  337. // WidthHeight - Dimentions Of The Picture To Draw (Width,Height)
  338. // MagnifyX - Magnify Pixel Width, 0 = Default (No Magnify)
  339. // MagnifyY - Magnify Pixel Height, 0 = Default (No Magnify)
  340. //
  341. // OutPut: TRUE If Succeeded...
  342. // ~~~~~~
  343. //-----------------------------------------------------------------------------
  344. BOOL CPicture::Show(CDC *pDC, CPoint LeftTop, CPoint WidthHeight, int MagnifyX, int MagnifyY)
  345. //=============================================================================
  346. {
  347. if (pDC == NULL || m_IPicture == NULL) return FALSE;
  348. long Width = 0;
  349. long Height = 0;
  350. m_IPicture->get_Width(&Width);
  351. m_IPicture->get_Height(&Height);
  352. if(MagnifyX == NULL) MagnifyX = 0;
  353. if(MagnifyY == NULL) MagnifyY = 0;
  354. MagnifyX = int(MulDiv(Width, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);
  355. MagnifyY = int(MulDiv(Height,pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);
  356. CRect DrawRect(LeftTop.x, LeftTop.y, MagnifyX, MagnifyY);
  357. HRESULT hrP = NULL;
  358. hrP = m_IPicture->Render(pDC->m_hDC,
  359. LeftTop.x, // Left
  360. LeftTop.y, // Top
  361. WidthHeight.x +MagnifyX, // Width
  362. WidthHeight.y +MagnifyY, // Height
  363. 0,
  364. Height,
  365. Width,
  366. -Height,
  367. &DrawRect);
  368. if(SUCCEEDED(hrP)) return(TRUE);
  369. #if 0
  370. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  371. MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  372. #endif
  373. return(FALSE);
  374. }
  375. //-----------------------------------------------------------------------------
  376. // Does: Saves The Picture That Is Stored In The IPicture Object As a Bitmap
  377. // ~~~~ (Converts From Any Known Picture Type To a Bitmap / Icon File)
  378. //
  379. // InPut: sFilePathName - Path And FileName Target To Save
  380. // ~~~~~
  381. //
  382. // OutPut: TRUE If Succeeded...
  383. // ~~~~~~
  384. //-----------------------------------------------------------------------------
  385. BOOL CPicture::SaveAsBitmap(CString sFilePathName)
  386. //=============================================================================
  387. {
  388. BOOL bResult = FALSE;
  389. ILockBytes *Buffer = 0;
  390. IStorage *pStorage = 0;
  391. IStream *FileStream = 0;
  392. BYTE *BufferBytes;
  393. STATSTG BytesStatistics;
  394. DWORD OutData;
  395. long OutStream;
  396. CFile BitmapFile; CFileException e;
  397. double SkipFloat = 0;
  398. DWORD ByteSkip = 0;
  399. _ULARGE_INTEGER RealData;
  400. CreateILockBytesOnHGlobal(NULL, TRUE, &Buffer); // Create ILockBytes Buffer
  401. HRESULT hr = ::StgCreateDocfileOnILockBytes(Buffer,
  402. STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage);
  403. hr = pStorage->CreateStream(L"PICTURE",
  404. STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, 0, &FileStream);
  405. m_IPicture->SaveAsFile(FileStream, TRUE, &OutStream); // Copy Data Stream
  406. FileStream->Release();
  407. pStorage->Release();
  408. Buffer->Flush();
  409. // Get Statistics For Final Size Of Byte Array
  410. Buffer->Stat(&BytesStatistics, STATFLAG_NONAME);
  411. // Cut UnNeeded Data Coming From SaveAsFile() (Leave Only "Pure" Picture Data)
  412. SkipFloat = (double(OutStream) / 512); // Must Be In a 512 Blocks...
  413. if(SkipFloat > DWORD(SkipFloat)) ByteSkip = (DWORD)SkipFloat + 1;
  414. else ByteSkip = (DWORD)SkipFloat;
  415. ByteSkip = ByteSkip * 512; // Must Be In a 512 Blocks...
  416. // Find Difference Between The Two Values
  417. ByteSkip = (DWORD)(BytesStatistics.cbSize.QuadPart - ByteSkip);
  418. // Allocate Only The "Pure" Picture Data
  419. RealData.LowPart = 0;
  420. RealData.HighPart = 0;
  421. RealData.QuadPart = ByteSkip;
  422. BufferBytes = (BYTE*)malloc(OutStream);
  423. if(BufferBytes == NULL)
  424. {
  425. Buffer->Release();
  426. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  427. MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  428. }
  429. Buffer->ReadAt(RealData, BufferBytes, OutStream, &OutData);
  430. if(BitmapFile.Open(sFilePathName, CFile::typeBinary | CFile::modeCreate | CFile::modeWrite, &e))
  431. {
  432. BitmapFile.Write(BufferBytes, OutData);
  433. BitmapFile.Close();
  434. bResult = TRUE;
  435. }
  436. else // Write File Failed...
  437. {
  438. TCHAR szCause[255];
  439. e.GetErrorMessage(szCause, 255, NULL);
  440. HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  441. MessageBoxEx(hWnd, szCause, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  442. bResult = FALSE;
  443. }
  444. Buffer->Release();
  445. free(BufferBytes);
  446. return(bResult);
  447. }
  448. //-----------------------------------------------------------------------------
  449. // Does: Draw a Bitmap Resource To The Client DC (Using Bitblt())
  450. // ~~~~ It Will Use The Bitmap Resource Original Size (Width And Height)
  451. // (.BMP .DIB)
  452. //
  453. // Note: This Function Is Just Another Simple Way Of Displaying a Bitmap Resource,
  454. // ~~~~ It Is Not Connected With The IPicture Interface And Can Be Used
  455. // As a StandAlone On Any Device Context (Directly)
  456. //
  457. // InPut: BMPResource - Resource Name As Defined In The Resources
  458. // ~~~~~ pDC - Given DC To Draw On
  459. // LeftTop - Opening Point To Start Drawing (Left,Top)
  460. //
  461. // OutPut: TRUE If Succeeded...
  462. // ~~~~~~
  463. //-----------------------------------------------------------------------------
  464. BOOL CPicture::ShowBitmapResource(CDC *pDC, const int BMPResource, CPoint LeftTop)
  465. //=============================================================================
  466. {
  467. if (pDC == NULL) return(FALSE);
  468. CBitmap BMP;
  469. if(BMP.LoadBitmap(BMPResource))
  470. {
  471. // Get Bitmap Details
  472. BITMAP BMPInfo;
  473. BMP.GetBitmap(&BMPInfo);
  474. // Create An In-Memory DC Compatible With The Display DC We R Gonna Paint On
  475. CDC DCMemory;
  476. DCMemory.CreateCompatibleDC(pDC);
  477. // Select The Bitmap Into The In-Memory DC
  478. CBitmap* pOldBitmap = DCMemory.SelectObject(&BMP);
  479. // Copy Bits From The In-Memory DC Into The On-Screen DC
  480. pDC->BitBlt(LeftTop.x, LeftTop.y, BMPInfo.bmWidth, BMPInfo.bmHeight, &DCMemory, 0, 0, SRCCOPY);
  481. DCMemory.SelectObject(pOldBitmap); // (As Shown In MSDN Example...)
  482. }
  483. else
  484. {
  485. TRACE0("ERROR: Can Not Find The Bitmap Resource\n");
  486. return(FALSE);
  487. }
  488. return(TRUE);
  489. }
  490. //-----------------------------------------------------------------------------
  491. // Does: Get The Original Picture Pixel Size (Ignor What Current DC Is Using)
  492. // ~~~~ Pointer To a Device Context Is Needed For Pixel Calculation,
  493. //
  494. // Also Updates The Class's Height And Width Properties,
  495. // (Coz Till Now We Had No Device Context To Work With...96 DPI Assumed)
  496. //
  497. // InPut: The Client DC (Needed To Check The Size Of The Pixels)
  498. // ~~~~~
  499. //
  500. // OutPut: TRUE If Succeeded...
  501. // ~~~~~~
  502. //-----------------------------------------------------------------------------
  503. BOOL CPicture::UpdateSizeOnDC(CDC *pDC)
  504. //=============================================================================
  505. {
  506. if(pDC == NULL || m_IPicture == NULL) { m_Height = 0; m_Width = 0; return(FALSE); };
  507. m_IPicture->get_Height(&m_Height);
  508. m_IPicture->get_Width(&m_Width);
  509. // Get Current DPI - Dot Per Inch
  510. int CurrentDPI_X = pDC->GetDeviceCaps(LOGPIXELSX);
  511. int CurrentDPI_Y = pDC->GetDeviceCaps(LOGPIXELSY);
  512. // Use a "Standard" Print (When Printing)
  513. if(pDC->IsPrinting())
  514. {
  515. CurrentDPI_X = 96;
  516. CurrentDPI_Y = 96;
  517. }
  518. m_Height = MulDiv(m_Height, CurrentDPI_Y, HIMETRIC_INCH);
  519. m_Width = MulDiv(m_Width, CurrentDPI_X, HIMETRIC_INCH);
  520. return(TRUE);
  521. }