Use GDI + to convert BMP bitmap to JPG Bitmap (and convert com Object IPicture to jpg)

Source: Internet
Author: User

First, the header file contains the GDI + header and library.

 

# Ifndef ULONG_PTR
# Define ULONG_PTR unsigned long *
# Include "GdiPlus. h"
Using namespace Gdiplus;
# Pragma comment (lib, "gdiplus. lib ")
# Include <atlconv. h>
# Endif

 

Define related member variables in the class

GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;

 

Initialize gdi +

GdiplusStartup (& m_gdiplusToken, & m_gdiplusStartupInput, NULL );

 

Destroy gdi + at end

GdiplusShutdown (m_gdipluw.en );

 

Conversion functions

BOOL CtojpgDlg: MBmpToMImage (CFile & cbfBmp, CFile & cbfImage, CString strType) <br/>{< br/> int iBmpSize = cbfBmp. getLength (); <br/> HGLOBAL hMemBmp = GlobalAlloc (GMEM_FIXED, iBmpSize); <br/> if (hMemBmp = NULL) return FALSE; <br/> IStream * pStmBmp = NULL; <br/> CreateStreamOnHGlobal (hMemBmp, FALSE, & pStmBmp); <br/> if (pStmBmp = NULL) <br/>{< br/> GlobalFree (hMemBmp); <br/> return FALSE; <br/>}< br/> BYTE * PbyBmp = (BYTE *) GlobalLock (hMemBmp); <br/> cbfBmp. seekToBegin (); <br/> cbfBmp. read (pbyBmp, iBmpSize); </p> <p> Image * imImage = NULL; <br/> imImage = Image: FromStream (pStmBmp, FALSE ); <br/> if (imImage = NULL) <br/>{< br/> GlobalUnlock (hMemBmp); <br/> GlobalFree (hMemBmp ); <br/> return FALSE; <br/>}< br/> USES_CONVERSION; <br/> CLSID clImageClsid; <br/> GetImageCLSID (_ T ("image/jpeg"), & clImageClsid); </p> <p> HGLOBAL hMemImage = GlobalAlloc (GMEM_MOVEABLE, 0); <br/> if (hMemImage = NULL) <br/>{< br/> pStmBmp-> Release (); <br/> GlobalUnlock (hMemBmp); <br/> GlobalFree (hMemBmp); <br/> if (imImage! = NULL) delete imImage; <br/> return FALSE; <br/>}< br/> IStream * pStmImage = NULL; <br/> CreateStreamOnHGlobal (hMemImage, TRUE, & pStmImage); <br/> if (pStmImage = NULL) <br/>{< br/> pStmBmp-> Release (); <br/> GlobalUnlock (hMemBmp ); <br/> GlobalFree (hMemBmp); <br/> GlobalFree (hMemImage); <br/> if (imImage! = NULL) delete imImage; <br/> return FALSE; <br/>}< br/> imImage-> Save (pStmImage, & clImageClsid ); <br/> if (pStmImage = NULL) <br/>{< br/> pStmBmp-> Release (); <br/> pStmImage-> Release (); <br/> GlobalUnlock (hMemBmp); <br/> GlobalFree (hMemImage); <br/> if (imImage! = NULL) delete imImage; <br/> return FALSE; <br/>}< br/> LARGE_INTEGER liBegin = {0}; <br/> pStmImage-> Seek (liBegin, STREAM_SEEK_SET, NULL); <br/> BYTE * pbyImage = (BYTE *) GlobalLock (hMemImage); <br/> cbfImage. seekToBegin (); <br/> cbfImage. write (pbyImage, GlobalSize (hMemImage); </p> <p> if (imImage! = NULL) delete imImage; <br/> pStmBmp-> Release (); <br/> pStmImage-> Release (); <br/> GlobalUnlock (hMemBmp ); <br/> GlobalUnlock (hMemImage); <br/> GlobalFree (hMemBmp); <br/> GlobalFree (hMemImage); <br/> return TRUE; <br/>}</p> <p> BOOL CtojpgDlg: GetImageCLSID (const WCHAR * format, CLSID * pCLSID) <br/>{< br/> UINT num = 0; <br/> UINT size = 0; </p> <p> ImageCodecInfo * pImageCodecInfo = NULL; <br/> GetImageEncodersSize (& num, & size); <br/> if (size = 0) {<br/> return FALSE; <br/>}< br/> pImageCodecInfo = (ImageCodecInfo *) (malloc (size); <br/> if (pImageCodecInfo = NULL) <br/> return FALSE; <br/> GetImageEncoders (num, size, pImageCodecInfo ); </p> <p> // Find for the support of format for image in the windows <br/> for (UINT I = 0; I <num; ++ I) <br/>{< br/> // MimeType: Depiction for the program image <br/> if (wcscmp (pImageCodecInfo [I]. mimeType, format) = 0) <br/>{< br/> * pCLSID = pImageCodecInfo [I]. clsid; <br/> free (pImageCodecInfo); <br/> return TRUE; <br/>}< br/> free (pImageCodecInfo ); <br/> return FALSE; <br/>}

 

The above method is applicable when you know the BMP bitmap file, while converting from IPicture to jpg bitmap requires a slight change

 

LONG ExportShowPic (LONG nPicIndex, LPCTSTR strSavePath) // nPicIndex is the index of the IPICTURE array object, <br/> {// strSavePath is the jpg image storage path <br/> if (m_pIPicture [nPicIndex] = NULL) <br/>{< br/> return FALSE; <br/>}< br/> BOOL bResult = FALSE; <br/> // int iBmpSize = cbfBmp. getLength (); <br/> HGLOBAL hMemBmp = GlobalAlloc (GMEM_MOVEABLE, 0); <br/> if (hMemBmp = NULL) return FALSE; <br/> IStream * pStmBmp = NULL; <br/> C ReateStreamOnHGlobal (hMemBmp, FALSE, & pStmBmp); <br/> if (pStmBmp = NULL) <br/>{< br/> GlobalFree (hMemBmp ); <br/> return FALSE; <br/>}< br/> // BYTE * pbyBmp = (BYTE *) GlobalLock (hMemBmp); <br/> // cbfBmp. seekToBegin (); <br/> // cbfBmp. read (pbyBmp, iBmpSize); <br/> long OutStream = 0; <br/> m_pIPicture [nPicIndex]-> SaveAsFile (pStmBmp, TRUE, & OutStream ); // Copy Data Stream </p> <p> Image * imImage = NUL L; <br/> imImage = Image: FromStream (pStmBmp, FALSE); <br/> if (imImage = NULL) <br/>{< br/> GlobalUnlock (hMemBmp); <br/> GlobalFree (hMemBmp); <br/> return FALSE; <br/>}< br/> USES_CONVERSION; <br/> CLSID clImageClsid; <br/> GetImageCLSID (_ T ("image/jpeg"), & clImageClsid ); </p> <p> HGLOBAL hMemImage = GlobalAlloc (GMEM_MOVEABLE, 0); <br/> if (hMemImage = NULL) <br/>{< br/> pStmBmp-> Release (); <br /> GlobalUnlock (hMemBmp); <br/> GlobalFree (hMemBmp); <br/> if (imImage! = NULL) delete imImage; <br/> return FALSE; <br/>}< br/> IStream * pStmImage = NULL; <br/> CreateStreamOnHGlobal (hMemImage, TRUE, & pStmImage); <br/> if (pStmImage = NULL) <br/>{< br/> pStmBmp-> Release (); <br/> GlobalUnlock (hMemBmp ); <br/> GlobalFree (hMemBmp); <br/> GlobalFree (hMemImage); <br/> if (imImage! = NULL) delete imImage; <br/> return FALSE; <br/>}< br/> imImage-> Save (pStmImage, & clImageClsid ); <br/> if (pStmImage = NULL) <br/>{< br/> pStmBmp-> Release (); <br/> pStmImage-> Release (); <br/> GlobalUnlock (hMemBmp); <br/> GlobalFree (hMemImage); <br/> if (imImage! = NULL) delete imImage; <br/> return FALSE; <br/>}< br/> LARGE_INTEGER liBegin = {0}; <br/> pStmImage-> Seek (liBegin, STREAM_SEEK_SET, NULL); <br/> BYTE * pbyImage = (BYTE *) GlobalLock (hMemImage); <br/> // cbfImage. seekToBegin (); <br/> // cbfImage. write (pbyImage, GlobalSize (hMemImage); <br/> CFile fileJpg; <br/> if (fileJpg. open (strSavePath, CFile: modeCreate | CFile: modeWrite) <br/>{< br/> fileJpg. SeekToBegin (); <br/> fileJpg. Write (pbyImage, GlobalSize (hMemImage); <br/>}< br/> if (imImage! = NULL) delete imImage; <br/> pStmBmp-> Release (); <br/> pStmImage-> Release (); <br/> GlobalUnlock (hMemBmp ); <br/> GlobalUnlock (hMemImage); <br/> GlobalFree (hMemBmp); <br/> GlobalFree (hMemImage); </p> <p> bResult = TRUE; </p> <p> return TRUE; <br/>}

 

 

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.