PNG is a true color lossless format. In practice it can accomplish a compression on standard photos of a factor of 2-3x with no quality loss with respect to the raw bitmap data.
JPEG is a true color lossy format. Depending on the settings the user can determine the tradeoff between quality and amount of compression, which can usually achieve a compression ratio well above 10x.
CLSID是指windows系统对于不同的应用程序,文件类型,OLE对象,特殊文件夹以及各种系统组件分配一个唯一表示它的ID代码,用于对其身份的标示和与其他对象进行区分。
msdn上有例子:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx
The function GetEncoderClsid in the following example receives the MIME type of an encoder and returns the class identifier (CLSID) of that encoder. The MIME types of the encoders built into Windows GDI+ are as follows:
image/bmp
image/jpeg
image/gif
image/tiff
image/png
介绍类和一些方法:
ImageCodecInfo类
The ImageCodecInfo class provides the necessary storage members and methods to retrieve all pertinent information about the installed image encoders and decoders (called codecs). Not inheritable.
Clsid方法
Gets or sets a Guid structure that contains a GUID that identifies a specific codec.
GetImageEncodersSize
The GetImageEncodersSize function gets the number of available image encoders and the total size of the array of ImageCodecInfo objects that is returned by the GetImageEncoders function.
GetImageEncoders
The GetImageEncoders function gets an array of ImageCodecInfo objects that contain information about the available image encoders.
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
}
先介绍几个函数:
Bitmap::FromHBITMAP
The Bitmap::FromHBITMAP method creates a Bitmap object based on a handle to a Windows Graphics Device Interface (GDI) bitmap and a handle to a GDI palette.
Bitmap的save方法
Saves this Image to the specified file, with the specified encoder and image-encoder parameters.
void BitmapToJpg(HBITMAP hbmpImage, int width, int height)
{
Bitmap *p_bmp = Bitmap::FromHBITMAP(hbmpImage, NULL);
CLSID jpgClsid;
int result = GetEncoderClsid(L"image/jpeg", &jpgClsid);
if(result != -1)
std::cout << "Encoder succeeded" << std::endl;
else
std::cout << "Encoder failed" << std::endl;
p_bmp->Save(L"screen.jpg", &pngClsid, NULL);
delete p_bmp;
}
先介绍几个函数:
CreateCompatibleDC
The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device.
CreateCompatibleBitmap
The CreateCompatibleBitmap function creates a bitmap compatible with the device that is associated with the specified device context.
BitBlt
The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.
bool ScreenCapture(int x, int y, int width, int height)
{
HDC hDc = CreateCompatibleDC(0);
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);
SelectObject(hDc, hBmp);
BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);
BitmapToJpg(hBmp, width, height);
DeleteObject(hBmp);
DeleteDC(hDc);
return true;
}