Use IStream and GDI + to convert image formats in memory

Source: Internet
Author: User

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 ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.