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")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, 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>
<DllImport("gdi32.dll")> _
Private Shared Function BitBlt(ByVal hdc As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As TernaryRasterOperations) As Boolean
End Function

User-Defined Types:

TernaryRasterOperations

Notes:

Use SelectObject to select a source image into the source DC before trying to BitBlt it.

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:

Here's a way to take a bitmap snapshot of any Windows.Forms.Control (PInvoke signatrue omitted for brevity)

C#

        private Bitmap SnapShot(win.Control c)
        {
            Bitmap        bmp;
            IntPtr        dc1;
            IntPtr        dc2;
            Graphics    g;

            bmp=new Bitmap(c.Width, c.Height);
            g=System.Drawing.Graphics.FromImage(bmp);

            dc1=g.GetHdc();
            dc2=GetWindowDC(c.Handle);

            BitBlt(dc1, 0, 0, c.Width, c.Height, dc2, 0, 0, 13369376);

            g.ReleaseHdc(dc1);

            return bmp;
            //bmp.Save(@"c:\snapshot.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
        }

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