[VC ++] a good class showing pictures ~

xiaoxiao2021-03-06  39

//// Class Declaration ~ // CLASS CMYPICTURE {public: cmypicture (); virtual ~ cmypicture ();

public: void FreePictureData (); BOOL Load (CString sFilePathName); BOOL Load (UINT ResourceName, LPCSTR ResourceType); BOOL LoadPictureData (BYTE * pBuffer, int nSize); BOOL SaveAsBitmap (CString sFilePathName); BOOL Show (CDC * pDC, CPoint LeftTop, CPoint WidthHeight, int MagnifyX, int MagnifyY); BOOL Show (CDC * pDC, CRect DrawRect); BOOL ShowBitmapResource (CDC * pDC, const int BMPResource, CPoint LeftTop); BOOL UpdateSizeOnDC (CDC * pDC);

public: IPicture * m_IPicture; // Same As LPPICTURE (typedef IPicture __RPC_FAR * LPPICTURE) LONG m_Height; // Height (In Pixels Ignor What Current Device Context Uses) LONG m_Weight; // Size Of The Image Object In Bytes (File OR Resource ) Long m_width; // width (in pixels ignor what current device; //// class definition ~ // # include "mypicture.h"

CMYPICTURE :: CMYPICTURE () {m_ipicture = null; m_height = 0; m_weiGHT = 0; m_width = 0;}

CMYPICTURE :: ~ cmypicture () {if (m_ipicture! = Null) freePictata (); // Avoid memory leaks ~}

Void CMYPICTA :: FreePictureData () {if (m_ipicture! = null) {m_ipicture-> release (); m_ipicture = null; m_height = 0; m_weart = 0;}}

Bool CMYPICTURE :: Load (uint resourceename, lpcstr resourcetype) {bool bresult = false;

HWND hWnd = AfxGetApp () -> GetMainWnd () -> m_hWnd; 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) {MessageBoxEx (hWnd, "FindResource () Failed / t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH); return FALSE;} hGlobal = LoadResource ( AfxGetResourceHandle (), hSource); if (hGlobal == NULL) {MessageBoxEx (hWnd, "LoadResource () Failed / t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH); return FALSE;} lpVoid = LockResource (hGlobal); if ( lpVoid == NULL) {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); // 16bit Windows Needs this freeeresource (hglobal); // 16bit Windows Needs th IS (32bit - Automatic Release) M_Weight = nsize; // Update Picture Size Info ... IF (m_ipicture! = null) // do not try to read from memory That IS not exist ... {m_ipicture-> get_height (& m_height ); m_IPicture-> get_Width (& m_Width); // Calculate Its Size On a "Standard" (96 DPI) Device Context m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH);} else // Picture data is not a known picture type {m_height = 0; m_width = 0; BRESULT = FALSE;

Return BRESULT;

BOOL CMyPicture :: Load (CString sFilePathName) {BOOL bResult = FALSE; CFile PictureFile; CFileException e; int nSize = 0; if (m_IPicture = NULL!) FreePictureData (); // Important - Avoid Leaks ... if (PictureFile. Open (sfilepathname, cfile :: mode = PictureFile.getLength (); byte * pBuffer = new byte [nsize]; // read from the file PBuffer if (PCUFEFILE.READ , nsize)> 0) {// Self-acceptance function read PBuffer if (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; // update picture size info ... if (m_ipicture! = Null) // do not try to read from memory That is not exist ... {m_ipicture-> get_height (& m_height); m_ipicture-> GET_WIDTH (& M_WIDTH);

// Calculate Its Size On a "Standard" (96 DPI) Device Context m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH);} else // unknown // Picture Data Is Not a KNOWN PICTURE TYPE {m_height = 0; m_width = 0; BRESULT = FALSE;} Return BRESULT;}

BOOL CMyPicture :: 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 *) & (this-> 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;}} freeeresource (hglobal); // 16bit Windows Needs this (32bit - Automatic Release) Return BRESULT;}

Bool CMYPICTURE :: Show (CDC * PDC, CRECT DRAWRECT) {// CDC Returns ~ 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.Bottom - DrawRect.top, //HeiGHT 0, Height, Width, -Height, & DrawRect; if (succeeded) Return true; // Unable to display picture 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 CMyPicture :: Show (CDC * pDC, CPoint Lefttop, cpoint widthheight, int marky = 0) {// CDC returns ~ if (PDC == null || m_ipicture == null) Return False; Long Width = 0; long heiGHT = 0; m_ipicture-> get_width (& width); M_IPIC Ture-> 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, // Top Widthheight.x Magnifyx, // Width Widthheight.y Magnify, // Height 0, Height, Width, -Height, & DrawRect); // If successful if (acue) 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 CMyPicture :: 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_IPicture-> 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) BYTE Skip = (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_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_title, mb_ok | MB_ICONSTOP, LANG_ENGLISH); BRESULT = false;} buffer-> Release (); free (bufferbytes); return bresult;}

BOOL CMyPicture :: ShowBitmapResource (CDC * pDC, const int BMPResource, CPoint LeftTop) {if (pDC == NULL) return (FALSE); CBitmap BMP; if (BMP.LoadBitmap (BMPResource)) {// Get Bitmap Details BITMAP BMPInfo ; BMP.GetBitmap (& BMPInfo); // Create An In-Memory DC Compatible With The Display DC We R Gonna Paint On CDC DCMemory; DCMemory.CreateCompatibleDC (pDC); // Select The Bitmap Into The In-Memory DC CBitmap * pOldBitmap = DCMemory.selectObject (& bmp); // Copy Bits from the in-screen DC INTO The on-screen DC PDC-> Bitblt (Lefttop.x, Lefttop.y, Bmpinfo.Bmwidth, Bmpinfo.bmheight, & Dcmemory, 0, 0 , SRCCopy; DCMemory.SelectObject (Poldbitmap); // (As Shown In MSDN Example ...)} else {trace ("Error: can not find the bitmap resource / n"); Return False;}

Return True;}

转载请注明原文地址:https://www.9cbs.com/read-69474.html

New Post(0)