First, call diplus. lib statically in StdAfx. h, that is, the compiling system loads the DLL and uninstalls the DLL encoding when the application ends. As follows:
# Ifndef ULONG_PTR
# Define ULONG_PTR unsigned long *
# Include "GdiPlus. h"
Using namespace Gdiplus;
# Pragma comment (lib, "gdiplus. lib ")
# Endif
The following member variables are defined in the Class header file to initialize the usage and end use of GDI +.
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
Then add the function that initializes GDI + to the OnCreate () function:
GdiplusStartup (& m_gdiplusToken, & m_gdiplusStartupInput, NULL );
In the OnDestroy () function, add the function used to end GDI +:
GdiplusShutdown (m_gdipluw.en );
Define the Conversion Function: BOOL MBmpToMImage (CMemFile & cbfBmp, CMemFile & cbfImage, CString strType)
Where:
CMemFile & cbfBmp indicates the original bitmap file;
CMemFile & cbfImage indicates the converted image file;
CString strType indicates the converted image type.
The main process of this function is as follows:
Convert the original bitmap file to IStream
Define the Image class instance and use the IStream initialization obtained in step 1.
Obtain the CLSID of the converted image type
Save the Image as the converted Image type to IStream.
Convert IStream to a CMemFile memory file (CFile can also be used)
The Code is as follows:
BOOL MBmpToMImage (CMemFile & cbfBmp, CMemFile & cbfImage, CString strType)
{
Int iBmpSize = cbfBmp. GetLength ();
HGLOBAL hMemBmp = GlobalAlloc (GMEM_FIXED, iBmpSize );
If (hMemBmp = NULL) return FALSE;
IStream * pStmBmp = NULL;
CreateStreamOnHGlobal (hMemBmp, FALSE, & pStmBmp );
If (pStmBmp = NULL)
{
GlobalFree (hMemBmp );
Return FALSE;
}
BYTE * pbyBmp = (BYTE *) GlobalLock (hMemBmp );
CbfBmp. SeekToBegin ();
CbfBmp. Read (pbyBmp, iBmpSize );
Image * imImage = NULL;
ImImage = Image: FromStream (pStmBmp, FALSE );
If (imImage = NULL)
{
GlobalUnlock (hMemBmp );
GlobalFree (hMemBmp );
Return FALSE;
}
USES_CONVERSION;
CLSID clImageClsid;
GetImageCLSID (A2W ("image/" + strType. GetBuffer (0), & clImageClsid );
HGLOBAL hMemImage = GlobalAlloc (GMEM_MOVEABLE, 0 );
If (hMemImage = NULL)
{
PStmBmp-> Release ();
GlobalUnlock (hMemBmp );
GlobalFree (hMemBmp );
If (imImage! = NULL) delete imImage;
Return FALSE;
}
IStream * pStmImage = NULL;
CreateStreamOnHGlobal (hMemImage, TRUE, & pStmImage );
If (pStmImage = NULL)
{
PStmBmp-> Release ();
GlobalUnlock (hMemBmp );
GlobalFree (hMemBmp );
GlobalFree (hMemImage );
If (imImage! = NULL) delete imImage
Return FALSE;
}
ImImage-> Save (pStmImage, & clJpgClsid );
If (pStmImage = NULL)
{
PStmBmp-> Release ();
PStmImage> Release ();
GlobalUnlock (hMemBmp );
GlobalFree (hMemBmp );
GlobalFree (hMemImage;
If (imImage! = NULL) delete imImage;
Return FALSE;
}
LARGE_INTEGER liBegin = {0 };
PStmImage-> Seek (liBegin, STREAM_SEEK_SET, NULL );
BYTE * pbyImage = (BYTE *) GlobalLock (hMemImage );
CbfImage. SeekToBegin ();
CbfImage. Write (pbyImage, GlobalSize (hMemImage ));
If (imImage! = NULL) delete imImage;
PStmBmp-> Release ();
PStmImage-> Release ();
GlobalUnlock (hMemBmp );
GlobalUnlock (hMemImage );
GlobalFree (hMemBmp );
GlobalFree (hMemImage );
Return TRUE;
}
IStream is an interface in COM. If you really don't understand it, You can ~~~