2 out of 6 rated this helpful Rate this topic

StretchDIBits function

The StretchDIBits function copies the color data for a rectangle of pixels in a DIB, JPEG, or PNG image 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.

Syntax

int StretchDIBits(
  __in  HDC hdc,
  __in  int XDest,
  __in  int YDest,
  __in  int nDestWidth,
  __in  int nDestHeight,
  __in  int XSrc,
  __in  int YSrc,
  __in  int nSrcWidth,
  __in  int nSrcHeight,
  __in  const VOID *lpBits,
  __in  const BITMAPINFO *lpBitsInfo,
  __in  UINT iUsage,
  __in  DWORD dwRop
);

Parameters

hdc [in]

A handle to the destination device context.

XDest [in]

The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.

YDest [in]

The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.

nDestWidth [in]

The width, in logical units, of the destination rectangle.

nDestHeight [in]

The height, in logical units, of the destination rectangle.

XSrc [in]

The x-coordinate, in pixels, of the source rectangle in the image.

YSrc [in]

The y-coordinate, in pixels, of the source rectangle in the image.

nSrcWidth [in]

The width, in pixels, of the source rectangle in the image.

nSrcHeight [in]

The height, in pixels, of the source rectangle in the image.

lpBits [in]

A pointer to the image bits, which are stored as an array of bytes. For more information, see the Remarks section.

lpBitsInfo [in]

A pointer to a BITMAPINFO structure that contains information about the DIB.

iUsage [in]

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.

ValueMeaning
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 Remarks section.

dwRop [in]

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 value

If the function succeeds, the return value is the number of scan lines copied. Note that this value can be negative for mirrored content.

If the function fails, or no scan lines are copied, the return value is 0.

If the driver cannot support the JPEG or PNG file image passed to StretchDIBits, the function will fail and return GDI_ERROR. If failure does occur, the application must fall back on its own JPEG or PNG support to decompress the image into a bitmap, and then pass the bitmap to StretchDIBits.

Remarks

The origin of a bottom-up DIB is the lower-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.

This function allows a JPEG or PNG image to be passed as the source image. How each parameter is used remains the same, except:

  • If the biCompression member of BITMAPINFOHEADER is BI_JPEG or BI_PNG, lpBits points to a buffer containing a JPEG or PNG image, respectively. The biSizeImage member of the BITMAPINFOHEADER structure 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 or CHECKPNGFORMAT escape to verify that the printer recognizes the JPEG or PNG image, respectively, before calling StretchDIBits.

ICM: Color management is performed if color management has been enabled with a call to SetICMMode with the iEnableICM parameter set to ICM_ON. If the bitmap specified by lpBitsInfo has a BITMAPV4HEADER that specifies the gamma and endpoints members, or a BITMAPV5HEADER that specifies either the gamma and endpoints members or the profileData and profileSize members, then the call treats the bitmap's pixels as being expressed in the color space described by those members, rather than in the device context's source color space.

Examples

For an example, see Sizing a JPEG or PNG Image.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Wingdi.h (include Windows.h)

Library

Gdi32.lib

DLL

Gdi32.dll

See also

Bitmaps Overview
Bitmap Functions
SetMapMode
SetStretchBltMode
BITMAPINFO

 

 

Send comments about this topic to Microsoft

Build date: 9/7/2011

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Origin is ALWAYS bottom-left
I have played a little with this function and it behaves identically no matter what sign the BITMAPINFOHEADER.biHeight has.
I  have tested with an uncompressed 24-bit bitmap on Windows Vista professional X64, VS2008 +MFC feature pack,MFC 32bit app.
The rectangle you give to select a part of image is origin-ed at image's bottom left.
Except if the source and destination rectangles are exactly the same(top,left,width,height are identical).
In this case, strechdibbits remembers that it's not doing the right thing and switches to top-left origin.
VERY NASTY BUG. 
I went around it by using SetDIBitsToDevice if the source and destination rects have the same size.
Have a look at the DIBLOOK :) sample for more inspiration.

The rectangle for the destination (window) is correctly origin-ed on the window's client area top-left (Mode MM_TEXT).