StretchDIBits

The StretchDIBits function copies the color data for a rectangle of pixels in a device-independent bitmap (DIB) to the specified destination rectangle. If the destination rectangle is larger than the source rectangle, this function stretches the rows and columns of color data to fit the destination rectangle. If the destination rectangle is smaller than the source rectangle, this function compresses the rows and columns by using the specified raster operation.

int StretchDIBits(
  HDC hdc,                // handle to device context
  int XDest,              // x-coordinate of upper-left corner of dest. rectangle
  int YDest,              // y-coordinate of upper-left corner of dest. rectangle
  int nDestWidth,         // width of destination rectangle
  int nDestHeight,        // height of destination rectangle
  int XSrc,               // x-coordinate of upper-left corner of source rectangle
  int YSrc,               // y-coordinate of upper-left corner of source rectangle
  int nSrcWidth,          // width of source rectangle
  int nSrcHeight,         // height of source rectangle
  CONST VOID *lpBits,            // address of bitmap bits
  CONST BITMAPINFO *lpBitsInfo,  // address of bitmap data
  UINT iUsage,                   // usage flags
  DWORD dwRop                    // raster operation code
);
 

Parameters

hdc
Handle to the destination device context.
XDest
Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
YDest
Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nDestWidth
Specifies the width, in logical units, of the destination rectangle.
nDestHeight
Specifies the height, in logical units, of the destination rectangle.
XSrc
Specifies the x-coordinate, in pixels, of the source rectangle in the DIB.
YSrc
Specifies the y-coordinate, in pixels, of the source rectangle in the DIB.
nSrcWidth
Specifies the width, in pixels, of the source rectangle in the DIB.
nSrcHeight
Specifies the height, in pixels, of the source rectangle in the DIB.
lpBits
Pointer to the DIB bits, which are stored as an array of bytes. For more information, see the following Remarks section.
lpBitsInfo
Pointer to a BITMAPINFO structure that contains information about the DIB.
iUsage
Specifies whether the bmiColors member of the BITMAPINFO structure was provided and, if so, whether bmiColors contains explicit red, green, blue (RGB) values or indexes. The iUsage parameter must be one of the following values.
Value Meaning
DIB_PAL_COLORS The array contains 16-bit indexes into the logical palette of the source device context.
DIB_RGB_COLORS The color table contains literal RGB values.

For more information, see the following Remarks section.

dwRop
Specifies how the source pixels, the destination device context's current brush, and the destination pixels are to be combined to form the new image. For more information, see the following Remarks section.

Return Values

If the function succeeds, the return value is the number of scan lines copied.

If the function fails, the return value is GDI_ERROR.

Windows NT: To get extended error information, call GetLastError.

Windows 98, Windows NT 5.0 and later: If the driver cannot support the JPEG file image passed to StretchDIBits, the function will fail and return GDI_ERROR.

Remarks

The origin of a bottom-up DIB is the bottom-left corner; the origin of a top-down DIB is the upper-left corner.

StretchDIBits creates a mirror image of a bitmap if the signs of the nSrcWidth and nDestWidth parameters, or if the nSrcHeight and nDestHeight parameters differ. If nSrcWidth and nDestWidth have different signs, the function creates a mirror image of the bitmap along the x-axis. If nSrcHeight and nDestHeight have different signs, the function creates a mirror image of the bitmap along the y-axis.

Windows 98, Windows NT 5.0 and later: This function allows a JPEG image to be passed as the source image. How each parameter is used remains the same.

If the biCompression member of BITMAPINFOHEADER is BI_JPEG, lpBits points to a buffer containing a JPEG image. The BITMAPINFOHEADER's biSizeImage member specifies the size of the buffer. The iUsage parameter must be set to DIB_RGB_COLORS. The dwRop parameter must be set to SRCCOPY.

If the bV4Compression member of BITMAPV4HEADER is BI_JPEG, lpBits points to a buffer containing a JPEG image. The BITMAPV4HEADER's bV4SizeImage member specifies the size of the buffer. The iUsage parameter must be set to DIB_RGB_COLORS. The dwRop parameter must be set to SRCCOPY.

If the bV5Compression member of BITMAPV5HEADER is BI_JPEG, lpBits points to a buffer containing a JPEG image. The BITMAPV5HEADER's bV5SizeImage member specifies the size of the buffer. The iUsage parameter must be set to DIB_RGB_COLORS. The dwRop parameter must be set to SRCCOPY.

To ensure proper metafile spooling while printing, applications must call the CHECKJPEGFORMAT escape to verify that the printer recognizes the JPEG image before calling StretchDIBits.

ICM: Color management is performed. If the specified BITMAPINFO's bmiHeader does not contain BITMAPV4HEADER or BITMAPV5HEADER, the color profile of the current device context is used as the source color space profile. If it does not have a color profile, the sRGB space is used. If the specified BITMAPINFO's bmiHeader contains BITMAPV4HEADER or BITMAPV5HEADER, the color space profile specified in the bitmap header is used as the source of color space profile.

QuickInfo

  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in wingdi.h.
  Import Library: Use gdi32.lib.

See Also

Bitmaps Overview, Bitmap Functions, SetMapMode, SetStretchBltMode, BITMAPINFO