Use GDI + to merge JPG images in VC
Merge two JPG images into one jpg image. The idea is to convert both JPG images into BMP images first, and then combine the two BMP images into one BMP image, then convert the BMP image into a jpg image.
I. JPG and BMP convert each other
/*********************************
Format: Convert BMP to JPG, format to image/JPEG, JPG to BMP, format to image/BMP
Strdst is the image path of the final Conversion Result
Strsrc is the path of the original image
**********************************/
Bool convertpic (const wchar * format, const cstring & strdst, const cstring & strsrc)
{
Bool bconvert = false;
CLSID;
Int nret = 0;
Nret = getencoderclsid (format, & CLSID); // obtain the CLSID
Uses_conversion;
If (nret> = 0)
{
Image image (a2w (strsrc ));
Image. Save (a2w (strdst), & CLSID, null );
Bconvert = true;
}
Return bconvert;
}
The getencoderclsid function is as follows:
/*************************************** **************
If the returned value is-1, it indicates failure. If the other value is success
**************************************** **************/
Int getencoderclsid (const wchar * format, CLSID * pclsid)
{
Int nret =-1;
Imagecodecinfo * pcodecinfo = NULL;
Uint nnum = 0, nsize = 0;
Getimageencoderssize (& nnum, & nsize );
If (nsize <0)
{
Return nret;
}
Pcodecinfo = new imagecodecinfo [nsize];
If (pcodecinfo = NULL)
{
Return nret;
}
Getimageencoders (nnum, nsize, pcodecinfo );
For (uint I = 0; I <nnum; I ++)
{
If (wcscmp (pcodecinfo [I]. mimetype, format) = 0)
{
* Pclsid = pcodecinfo [I]. CLSID;
Nret = I;
Delete [] pcodecinfo;
Return nret;
}
Else
{
Continue;
}
}
Delete [] pcodecinfo;
Return nret;
}
Convert BMP to JPG
Convertpic (L "image/JPEG", "C: // 1.jpg"," C: // 1.bmp ")
Convert JPG to BMP
Convertpic (L "image/BMP", "C: // 1.bmp", "C: // 1.jpg ")
II. BMP image Merging
Bool combinepic (const wchar * format, const cstring & strdst, const cstring & strpic1 ,/
Const cstring & strpic2)
{
Bool bcombine = false;
Int nret = 0;
CLSID;
Nret = getencoderclsid (format, & CLSID );
If (nret> = 0)
{
Uses_conversion;
Bitmap BMP 1 (a2w (strpic1 ));
Bitmap BMP 2 (a2w (strpic2 ));
Int nwidth = 0, nheight = 0;
Nwidth = BMP 1.getwidth (); // assume the two images are of the same size.
Nheight = BMP 1.getheight ();
Bitmap BMP combine (2 * nwidth, nheight); // height unchanged, width * 2, horizontal merge
Graphics * Pg = NULL;
Pg = graphics: fromimage (& BMP combine );
If (PG! = NULL)
{
Pg-> drawimage (& BMP 1, 0, 0 );
Pg-> drawimage (& BMP 2, nwidth, 0 );
BMP combine. Save (a2w (strdst), & CLSID, null );
}
}
Return bcombine;
}
Example:
Combinepic (L "image/BMP", "12.bmp", "1.bmp", "2.bmp ");