Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

BitBlt (gdi32)
 
.

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.

C# Signature:

/// <summary>
///    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.
/// </summary>
/// <param name="hdc">Handle to the destination device context.</param>
/// <param name="nXDest">The leftmost x-coordinate of the destination rectangle (in pixels).</param>
/// <param name="nYDest">The topmost y-coordinate of the destination rectangle (in pixels).</param>
/// <param name="nWidth">The width of the source and destination rectangles (in pixels).</param>
/// <param name="nHeight">The height of the source and the destination rectangles (in pixels).</param>
/// <param name="hdcSrc">Handle to the source device context.</param>
/// <param name="nXSrc">The leftmost x-coordinate of the source rectangle (in pixels).</param>
/// <param name="nYSrc">The topmost y-coordinate of the source rectangle (in pixels).</param>
/// <param name="dwRop">A raster-operation code.</param>
/// <returns>
///    <c>true</c> if the operation succeeded, <c>false</c> otherwise.
/// </returns>
[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

The last argument uint dwRop from the signature above specifies a raster operation code defined in the header. A value from enum TernaryRasterOperations below can be used as the last argument to fake the defined value as seen in this signature:

[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);

VB.NET Signature:

''' <summary>
'''    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.
''' </summary>
''' <param name="hdc">Handle to the destination device context.</param>
''' <param name="nXDest">The leftmost x-coordinate of the destination rectangle (in pixels).</param>
''' <param name="nYDest">The topmost y-coordinate of the destination rectangle (in pixels).</param>
''' <param name="nWidth">The width of the source and destination rectangles (in pixels).</param>
''' <param name="nHeight">The height of the source and the destination rectangles (in pixels).</param>
''' <param name="hdcSrc">Handle to the source device context.</param>
''' <param name="nXSrc">The leftmost x-coordinate of the source rectangle (in pixels).</param>
''' <param name="nYSrc">The topmost y-coordinate of the source rectangle (in pixels).</param>
''' <param name="dwRop">A raster-operation code.</param>
''' <returns>
'''    <c>true</c> if the operation succeeded, <c>false</c> otherwise.
''' </returns>
Public Declare Function BitBlt Lib "gdi32.dll" (hdc As IntPtr , nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As Integer, hdcSrc As IntPtr, nXSrc As Integer, nYSrc As Integer, dwRop As TernaryRasterOperations) As Boolean

User-Defined Types:

C#:

/// <summary>
///     Specifies a raster-operation code. These codes define how the color data for the
///     source rectangle is to be combined with the color data for the destination
///     rectangle to achieve the final color.
/// </summary>
public enum TernaryRasterOperations : uint {
    /// <summary>dest = source</summary>
    SRCCOPY     = 0x00CC0020,
    /// <summary>dest = source OR dest</summary>
    SRCPAINT    = 0x00EE0086,
    /// <summary>dest = source AND dest</summary>
    SRCAND      = 0x008800C6,
    /// <summary>dest = source XOR dest</summary>
    SRCINVERT   = 0x00660046,
    /// <summary>dest = source AND (NOT dest)</summary>
    SRCERASE    = 0x00440328,
    /// <summary>dest = (NOT source)</summary>
    NOTSRCCOPY  = 0x00330008,
    /// <summary>dest = (NOT src) AND (NOT dest)</summary>
    NOTSRCERASE = 0x001100A6,
    /// <summary>dest = (source AND pattern)</summary>
    MERGECOPY   = 0x00C000CA,
    /// <summary>dest = (NOT source) OR dest</summary>
    MERGEPAINT  = 0x00BB0226,
    /// <summary>dest = pattern</summary>
    PATCOPY     = 0x00F00021,
    /// <summary>dest = DPSnoo</summary>
    PATPAINT    = 0x00FB0A09,
    /// <summary>dest = pattern XOR dest</summary>
    PATINVERT   = 0x005A0049,
    /// <summary>dest = (NOT dest)</summary>
    DSTINVERT   = 0x00550009,
    /// <summary>dest = BLACK</summary>
    BLACKNESS   = 0x00000042,
    /// <summary>dest = WHITE</summary>
    WHITENESS   = 0x00FF0062
}

VB.NET:

public enum TernaryRasterOperations
    SRCCOPY     = &H00CC0020 ' dest = source
    SRCPAINT    = &H00EE0086 ' dest = source OR dest
    SRCAND      = &H008800C6 ' dest = source AND dest
    SRCINVERT   = &H00660046 ' dest = source XOR dest
    SRCERASE    = &H00440328 ' dest = source AND (NOT dest )
    NOTSRCCOPY  = &H00330008 ' dest = (NOT source)
    NOTSRCERASE = &H001100A6 ' dest = (NOT src) AND (NOT dest)
    MERGECOPY   = &H00C000CA ' dest = (source AND pattern)
    MERGEPAINT  = &H00BB0226 ' dest = (NOT source) OR dest
    PATCOPY     = &H00F00021 ' dest = pattern
    PATPAINT    = &H00FB0A09 ' dest = DPSnoo
    PATINVERT   = &H005A0049 ' dest = pattern XOR dest
    DSTINVERT   = &H00550009 ' dest = (NOT dest)
    BLACKNESS   = &H00000042 ' dest = BLACK
    WHITENESS   = &H00FF0062 ' dest = WHITE
End Enum

Notes:

BitBlt only does clipping on the destination DC.

If a rotation or shear transformation is in effect in the source device context, BitBlt returns an error. If other transformations exist in the source device context (and a matching transformation is not in effect in the destination device context), the rectangle in the destination device context is stretched, compressed, or rotated, as necessary.

If the color formats of the source and destination device contexts do not match, the BitBlt function converts the source color format to match the destination format.

When an enhanced metafile is being recorded, an error occurs if the source device context identifies an enhanced-metafile device context.

Not all devices support the BitBlt function. For more information, see the RC_BITBLT raster capability entry in the GetDeviceCaps function as well as the following functions: MaskBlt, PlgBlt, and StretchBlt.

BitBlt returns an error if the source and destination device contexts represent different devices. To transfer data between device contexts for different devices, convert the memory bitmap to a DIB by calling GetDIBits. To display the DIB to the second device, call SetDIBits or StretchDIBits.

Tips & Tricks:

Please add some!

Sample Code:

The BitBlt function can be used to quickly render a Bitmap onto a Control (and much, much more). For this purpose, it is much faster than the managed alternative, Graphics.DrawImage(). See the example code below.

[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
   int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc,  TernaryRasterOperations dwRop);    

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern bool DeleteObject(IntPtr hObject);

public enum TernaryRasterOperations : uint {
    SRCCOPY     = 0x00CC0020,
    SRCPAINT    = 0x00EE0086,
    SRCAND      = 0x008800C6,
    SRCINVERT   = 0x00660046,
    SRCERASE    = 0x00440328,
    NOTSRCCOPY  = 0x00330008,
    NOTSRCERASE = 0x001100A6,
    MERGECOPY   = 0x00C000CA,
    MERGEPAINT  = 0x00BB0226,
    PATCOPY     = 0x00F00021,
    PATPAINT    = 0x00FB0A09,
    PATINVERT   = 0x005A0049,
    DSTINVERT   = 0x00550009,
    BLACKNESS   = 0x00000042,
    WHITENESS   = 0x00FF0062
}

protected override void OnPaint(PaintEventArgs e) {
    IntPtr pTarget = e.Graphics.GetHdc();
    IntPtr pSource = CreateCompatibleDC(pTarget);
    IntPtr pOrig = SelectObject(pSource, bmp.GetHbitmap());
    BitBlt(pTarget, 0,0, bmp.Width, bmp.Height, pSource,0,0,TernaryRasterOperations.SRCCOPY);
    IntPtr pNew = SelectObject(pSource, pOrig);
    DeleteObject(pNew);
    DeleteDC(pSource);
    e.Graphics.ReleaseHdc(pTarget);
}

Alternative Managed API:

System.Drawing.Graphics.DrawImage

Documentation
BitBlt on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions