ImageHandle_IPicture.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (C) =USTC= Fu Li
  3. *
  4. * Author : Fu Li
  5. * Create : 2005-9-12
  6. * Home : http://www.crazy-bit.com/
  7. * Mail : crazybit@263.net
  8. * History :
  9. */
  10. #ifndef __FOO_IMAGE_HANDLE_IPICTURE__2005_09_12__H__
  11. #define __FOO_IMAGE_HANDLE_IPICTURE__2005_09_12__H__
  12. #include "../ObjImage.h"
  13. #include <ocidl.h>
  14. //class FCImageHandle ;
  15. class FCImageHandle_IPicture ;
  16. //=============================================================================
  17. /**
  18. * Use windows IPicture interface to read JPG/GIF image.
  19. */
  20. class FCImageHandle_IPicture : public FCImageHandleBase
  21. {
  22. /// Read image via IPicture interface.
  23. virtual bool LoadImageMemory (const BYTE* pStart, int nMemSize,
  24. PCL_Interface_Composite<FCObjImage>& rImageList,
  25. std::auto_ptr<FCImageProperty>& rImageProp)
  26. {
  27. if (!pStart || (nMemSize <= 0))
  28. return false ;
  29. HGLOBAL hBuffer = ::GlobalAlloc (GMEM_MOVEABLE, nMemSize) ;
  30. // copy buffer to HGLOBAL
  31. ::CopyMemory (::GlobalLock(hBuffer), pStart, nMemSize) ;
  32. ::GlobalUnlock (hBuffer) ;
  33. IStream * pStream = NULL ;
  34. IPicture * pIPic = NULL ;
  35. FCObjImage * pImg = new FCObjImage ;
  36. if (::CreateStreamOnHGlobal (hBuffer, TRUE, &pStream) == S_OK)
  37. {
  38. if (::OleLoadPicture (pStream, nMemSize, FALSE, IID_IPicture, (LPVOID*)&pIPic) == S_OK)
  39. {
  40. OLE_HANDLE hDDB ;
  41. pIPic->get_Handle (&hDDB) ;
  42. BITMAP bm ;
  43. GetObject ((HGDIOBJ)hDDB, sizeof(BITMAP), &bm) ;
  44. if (pImg->Create (bm.bmWidth, bm.bmHeight, 24))
  45. {
  46. // dest format
  47. PCL_array<BITMAPINFO> bmfh (pImg->NewImgInfoWithPalette()) ;
  48. HDC hdc = ::GetDC(NULL) ;
  49. ::GetDIBits (hdc, (HBITMAP)hDDB, 0, bm.bmHeight, pImg->GetMemStart(), bmfh.get(), DIB_RGB_COLORS) ;
  50. ::ReleaseDC (NULL, hdc) ;
  51. }
  52. pIPic->Release() ;
  53. }
  54. pStream->Release() ;
  55. }
  56. if (!pImg->IsValidImage())
  57. {
  58. delete pImg ;
  59. return false ;
  60. }
  61. // add to list
  62. rImageList.PCL_PushObject (pImg) ;
  63. return true ;
  64. }
  65. };
  66. //=============================================================================
  67. // inline Implement
  68. //=============================================================================
  69. #endif