"VC + + Technology 007" using GDI + for image format conversion

Source: Internet
Author: User

This article mainly describes how to use GDI + to format the image, you can convert the image format is BMP, JPG, PNG.

1. Loading GDI + libraries

GDI + is an enhanced version of the GDI graphics Library and provides a series of Visual C + + APIs. In order to use GDI +, you need to include the "GdiPlus.h" header file in your project and load the "gdiplus.lib" library file.

The specific implementation method is to download the GDI + library and put the downloaded Gdiplus folder into the project. The GdiPlus folder should contain a series of header files such as "GdiPlus.h", Gdiplus.lib library files, and Gdiplus.dll dynamic link libraries.

Add the following code to the "StdAfx.h" header file in the project:

1 #define UNICODE2#ifndef ulong_ptr3#define ulong_ptr unsigned long*4#endif  5"gdiplus/gdiplus.h"6usingnamespace  Gdiplus; 7 #pragma comment (lib, ". \\Gdiplus\\gdiplus.lib")

2. Initializing GDI + libraries and uninstalling GDI + libraries

The initialization of the GDI + library is implemented by adding the following code to the initialization start function InitInstance () of the project.

1     gdiplusstartupinput gdiplusstartupinput; 2     Gdiplusstartup (&m_gdiplustoken, &gdiplusstartupinput, NULL);

Where M_gdiplustoken is a member variable of type ULONG_PTR.

In the application exit, the ExitInstance () function, the implementation of the GDI + library unloading, the implementation code is as follows.

1     Gdiplusshutdown (M_gdiplustoken);

3. Programming examples

The following programming example enables the conversion of BMP, JPG, PNG three image formats to each other. The program runs as shown in interface 1.

Figure 1 main interface

3.1bitmap.save () method

The image format conversion mainly exploits the Save () method of the GDI + Bitmap object bitmap. This function can write a bitmap object to a disk file or data stream in the specified transcoding mode, where a function prototype is:

1 inline Status2 Image::save (3InchConstwchar*filename,4InchConstclsid*Clsidencoder,5InchConstEncoderParameters *Encoderparams6     )7 {8     returnSetStatus (Dllexports::gdipsaveimagetofile (Nativeimage,9 filename,Ten Clsidencoder, One encoderparams)); A}

Where the parameter filename means the disk file name (full path) after transcoding, the parameter Clsidencoder means the Transcoding method, and the parameter encoderparams represents the transcoding parameter.

3.2 Concrete Implementations

The following code example describes how to set the Transcoding method, transcode parameters, and call the Save () method of bitmap to complete the image format conversion after clicking the "Format Conversion" button.

1 /*2 * Function function: When the "Format conversion" button is clicked, the function is called3 * Remark:4 * Author: Blog Park is still indifferent5  */6 voidCimageconvertdlg::onbuttonimageconvert ()7 {8 UpdateData (TRUE);9 Ten     //detects if the source picture file is selected One     if(M_editimagepath.isempty ()) A     { -MessageBox ("Please select a picture!","Tips", mb_ok|mb_iconwarning); -         return; the     } -  -     //detect if the format is the same before and after conversion -     if(M_sourceimagetype = =M_destinationimagetype) +     { -MessageBox ("same format, no conversion required!","Tips", mb_ok|mb_iconwarning); +         return; A     } at      -     //Setting the Encoding method - CLSID CLSID; -     if(M_destinationimagetype = = _t ("BMP")) -Getencoderclsid (L"image/bmp", &clsid); -     Else if(M_destinationimagetype = = _t ("jpg")) inGetencoderclsid (L"Image/jpeg", &clsid); -     Else if(M_destinationimagetype = = _t ("PNG")) toGetencoderclsid (L"Image/png", &clsid); +  -     //Setting encoding Parameters theULONG encoderquality = -;//Compression ratio * encoderparameters encoderparameters; $Encoderparameters.count =1;Panax Notoginsengencoderparameters.parameter[0]. Guid =encoderquality; -encoderparameters.parameter[0]. Type =Encoderparametervaluetypelong; theencoderparameters.parameter[0]. NumberOfValues =1; +encoderparameters.parameter[0]. Value = &encoderquality;  A  the     //to convert a picture format +Bitmap Bmbitmap (m_editimagepath.allocsysstring ());//build a GDI + Bitmap object from the source picture file -CString Strsavesavefilepath = M_editimagepath.left (M_editimagepath.getlength ()-3); $Strsavesavefilepath + =M_destinationimagetype; $Status status = Bmbitmap.save (Strsavesavefilepath.allocsysstring (), &clsid, &encoderparameters); -     if(Status! =Ok) -     { theMessageBox ("picture conversion failed!","Tips", mb_ok|mb_iconwarning); -         return;Wuyi     } the}

Where the member variables M_sourceimagetype and m_destinationimagetype represent the source picture format and the destination picture format, respectively.

"VC + + Technology 007" using GDI + for image format conversion

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.