Recently, it is required to convert BMP to JPEG format. Previously, we used the GDI + method. This method has a major defect in that it cannot implement cross-platform processing. If you have less time to talk about it, go to the topic.
Cximage cximagebmp (prgbbuffer, dwrgbsize, cximage_format_bmp );
Cximagebmp. Save ("D: \ ttt.jpg", cximage_format_jpg );
This method is normal. The 24-bit BMP image in RGB format is successfully converted to a local JPG file and saved.
However, the following memory conversion method fails.
Byte * pjpgdatatemp = new byte [1024*1024];
Int ljpgsize = 0;
Bool B = cximagebmp. encode (pjpgdatatemp, ljpgsize, cximage_format_jpg );
On the one hand, the return value of the function is false; on the other hand, the Data Length, ljpgsize is 0.
Later, the modification was successful.
Byte * pjpgdatatemp = NULL;
Int ljpgsize = 0;
Bool B = cximagebmp. encode (pjpgdatatemp, ljpgsize, cximage_format_jpg );
Why? We can view the following information from the cximage: encode () Definition:
Bool cximage: encode (byte * & buffer, long & size, DWORD imagetype)
{
If (buffer! = NULL)
{
Strcpy (info. szlasterror, "the buffer must be empty ");
Return false;
}
Cxmemfile file;
File. open ();
If (encode (& file, imagetype ))
{
Buffer = file. getbuffer ();
Size = file. Size ();
Return true;
}
Return false;
}
The truth is clear!