以前看过MSDN里的声明SetDIBitsToDevice和StretchDIBits函数可以转化PNG和JPG图像,一直没有试验过,今天按MSDN的代码竟然试了几次都没有成功。呵呵,不知哪里有问题。
JPEG and PNG Extensions for Specific Bitmap Functions and Structures
On certain versions of Microsoft® Windows®, the StretchDIBits and SetDIBitsToDevice functions allow JPEG and PNG images to be passed as the source image to printer devices. This extension is not intended as a means to supply general JPEG and PNG decompression to applications, but rather to allow applications to send JPEG- and PNG-compressed images directly to printers having hardware support for JPEG and PNG images.
The BITMAPINFOHEADER, BITMAPV4HEADER and BITMAPV5HEADER structures are extended to allow specification of biCompression values indicating that the bitmap data is a JPEG or PNG image. These compression values are only valid for SetDIBitsToDevice and StretchDIBits when the hdc parameter specifies a printer device. To support metafile spooling of the printer, the application should not rely on the return value to determine whether the device supports the JPEG or PNG file. The application must issue QUERYESCSUPPORT with the corresponding escape before calling SetDIBitsToDevice and StretchDIBits. If the validation escape fails, the application must then fall back on its own JPEG or PNG support to decompress the image into a bitmap.
Testing a Printer for JPEG or PNG Support
The SetDIBitsToDevice function uses color data from a DIB to set the pixels in the specified rectangle on the device that is associated with the destination device context.
Windows 98/Me, Windows 2000/XP: SetDIBitsToDevice is extended to allow a JPEG or PNG image to be passed as the source image.
For example:
//
// pvJpgImage points to a buffer containing the JPEG image
// nJpgImageSize is the size of the buffer
// ulJpgWidth is the width of the JPEG image
// ulJpgHeight is the height of the JPEG image
//
//
// Check if CHECKJPEGFORMAT is supported (device has JPEG support)
// and use it to verify that device can handle the JPEG image.
//
ul = CHECKJPEGFORMAT;
if (
// Check if CHECKJPEGFORMAT exists:
(ExtEscape(hdc, QUERYESCSUPPORT,
sizeof(ul), &ul, 0, 0) > 0) &&
// Check if CHECKJPEGFORMAT executed without error:
(ExtEscape(hdc, CHECKJPEGFORMAT,
pvJpgImage, nJpgImageSize, sizeof(ul), &ul) > 0) &&
// Check status code returned by CHECKJPEGFORMAT:
(ul == 1)
)
{
//
// Initialize the BITMAPINFO.
//
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = ulJpgWidth;
bmi.bmiHeader.biHeight = -ulJpgHeight; // top-down image
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 0;
bmi.bmiHeader.biCompression = BI_JPEG;
bmi.bmiHeader.biSizeImage = nJpgImageSize;
//
// Do the SetDIBitsToDevice.
//
iRet = SetDIBitsToDevice(hdc,
ulDstX, ulDstY,
ulDstWidth, ulDstHeight,
0, 0,
0, ulJpgHeight,
pvJpgImage,
&bmi,
DIB_RGB_COLORS);
if (iRet == GDI_ERROR)
return FALSE;
}
else
{
//
// Decompress image into a DIB and call SetDIBitsToDevice
// with the DIB instead.
//
}