Convert BMP to jpg image through GDI +

Source: Internet
Author: User
Tags function examples

 

Start with a C/C ++ source code and convert the image format in the main () function ~

// C/C ++ code
# Include <windows. h>
# Include <gdiplus. h>
# Include <stdio. h>
Using namespace gdiplus;

Int getencoderclsid (const wchar * format, CLSID * pclsid); // helper Function

Int main ()
{
// Initialize GDI +.
Gdiplusstartupinput;
Ulong_ptr gdiplustoken;
Gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, null );

CLSID encoderclsid;
Encoderparameters;
Ulong quality;
Status Stat;

// Get an image from the disk.
Image * image = new image (L "shapes.bmp ");

// Get the CLSID of the JPEG encoder.
Getencoderclsid (L "image/JPEG", & encoderclsid );


// Before we call image: Save, We must Initialize
// Encoderparameters object. The encoderparameters object
// Has an array of encoderparameter objects. In this
// Case, there is onLy onE encoderparameter object in the array.
// The onE encoderparameter object has an array of values.
// In this case, there is onLy onE value (of Type ulong)
// In the array. We will let this value vary from 0 to 100.

Encoderparameters. Count = 1;
Encoderparameters. Parameter [0]. guid = encoderquality;
Encoderparameters. Parameter [0]. type = encoderparametervaluetypelong;
Encoderparameters. Parameter [0]. numberofvalues = 1;

// Save the image as a JPEG with quality level 0.
Quality = 0;
Encoderparameters. Parameter [0]. value = & quality;
Stat = image-> Save (L "shapes001.jpg", & encoderclsid, & encoderparameters );

If (STAT = OK)
Wprintf (L "% s saved successfully. \ n", l "shapes001.jpg ");
Else
Wprintf (L "% d attempt to save % s failed. \ n", stat, l "shapes001.jpg ");

// Save the image as a JPEG with quality level 50.
Quality = 50;
Encoderparameters. Parameter [0]. value = & quality;
Stat = image-> Save (L "shapes050.jpg", & encoderclsid, & encoderparameters );

If (STAT = OK)
Wprintf (L "% s saved successfully. \ n", l "shapes050.jpg ");
Else
Wprintf (L "% d attempt to save % s failed. \ n", stat, l "shapes050.jpg ");

// Save the image as a JPEG with quality level 100.
Quality = 100;
Encoderparameters. Parameter [0]. value = & quality;
Stat = image-> Save (L "shapes100.jpg", & encoderclsid, & encoderparameters );

If (STAT = OK)
Wprintf (L "% s saved successfully. \ n", l "shapes100.jpg ");
Else
Wprintf (L "% d attempt to save % s failed. \ n", stat, l "shapes100.jpg ");

Delete image;
Gdiplusshutdown (gdiplustoken );
Return 0;
}

Int getencoderclsid (const wchar * format, CLSID * pclsid). The function is a function in helper in Microsoft platform SDK. If not, add the following code implementation:

Int getencoderclsid (const wchar * format, CLSID * pclsid)

{
Uint num = 0; // Number of image encoders
Uint size = 0; // size of the image encoder array in bytes
Imagecodecinfo * pimagecodecinfo = NULL;
Getimageencoderssize (& num, & size );
If (size = 0)
Return-1; // failure

Pimagecodecinfo = (imagecodecinfo *) (malloc (size ));
If (pimagecodecinfo = NULL)
Return-1; // failure

Getimageencoders (Num, size, pimagecodecinfo );
For (uint J = 0; j <num; ++ J)
{
If (wcscmp (pimagecodecinfo [J]. mimetype, format) = 0)
{
* Pclsid = pimagecodecinfo [J]. CLSID;
Free (pimagecodecinfo );
Return J; // success
}
}
Free (pimagecodecinfo );
Return-1; // failure
}

Of course, we can also encapsulate them in the form of functions. The following are some simple function examples. You can modify them as needed:

// Save a bitmap * as a jpg image

Void SaveFile (Bitmap * pimage, const wchar_t * pfilename)
{
Encoderparameters;
CLSID jpgclsid; getencoderclsid (L "image/JPEG", & jpgclsid );
Encoderparameters. Count = 1;
Encoderparameters. Parameter [0]. guid = encoderquality;
Encoderparameters. Parameter [0]. type = encoderparametervaluetypelong;
Encoderparameters. Parameter [0]. numberofvalues = 1;

// Save the image as a JPEG with quality level 100.
Ulong quality;
Quality = 100;
Encoderparameters. Parameter [0]. value = & quality;
Status status = pimage-> Save (pfilename, & jpgclsid, & encoderparameters );
If (status! = OK)
{
Wprintf (L "% d attempt to save % s failed. \ n", status, pfilename );
}
}


 

// Save the current screen as a jpg image

// Parameter xs = image X axis size, ys = image Y axis size, quality = JPEG Image Quality
Void savecurscreenjpg (lpcwstr pszfilename, int XS, int ys, int quality)

{
Hwnd =: getdomaintopwindow ();
HDC = getwindowdc (null );
Int x = getdevicecaps (HDC, horzres );
Int y = getdevicecaps (HDC, vertres );
Hbitmap hbmp =: createcompatiblebitmap (HDC, x, y), hold;
HDC hmemdc =: createcompatibledc (HDC );
Hold = (hbitmap): SelectObject (hmemdc, hbmp );
Bitblt (hmemdc, 0, 0, x, y, HDC, 0, 0, srccopy );
SelectObject (hmemdc, hold );

Bitmap bit (XS, ys), bit2 (hbmp, null );
Graphics g (& bit );
G. scaletransform (float) XS/X, (float) YS/y );
G. drawimage (& bit2, 0, 0 );

CLSID encoderclsid;
Encoderparameters;

Encoderparameters. Count = 1;
Encoderparameters. Parameter [0]. guid = encoderquality;
Encoderparameters. Parameter [0]. type = encoderparametervaluetypelong;
Encoderparameters. Parameter [0]. numberofvalues = 1;
Encoderparameters. Parameter [0]. value = & quality;

Getencoderclsid (L "image/JPEG", & encoderclsid );
Bit. Save (pszfilename, & encoderclsid, & encoderparameters );

: Deleteobject (hbmp );
: Deleteobject (hmemdc );
Return;
}

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.