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:

[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:

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#:

public enum TernaryRasterOperations
{
    SRCCOPY     = 0x00CC0020, /* dest = source*/
    SRCPAINT    = 0x00EE0086, /* dest = source OR dest*/
    SRCAND      = 0x008800C6, /* dest = source AND dest*/
    SRCINVERT   = 0x00660046, /* dest = source XOR dest*/
    SRCERASE    = 0x00440328, /* dest = source AND (NOT dest )*/
    NOTSRCCOPY  = 0x00330008, /* dest = (NOT source)*/
    NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
    MERGECOPY   = 0x00C000CA, /* dest = (source AND pattern)*/
    MERGEPAINT  = 0x00BB0226, /* dest = (NOT source) OR dest*/
    PATCOPY     = 0x00F00021, /* dest = pattern*/
    PATPAINT    = 0x00FB0A09, /* dest = DPSnoo*/
    PATINVERT   = 0x005A0049, /* dest = pattern XOR dest*/
    DSTINVERT   = 0x00550009, /* dest = (NOT dest)*/
    BLACKNESS   = 0x00000042, /* dest = BLACK*/
    WHITENESS   = 0x00FF0062, /* dest = WHITE*/
};

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:

This can be used to replace DrawImage() in the managed api since it is much faster!!!

Tips & Tricks:

Please add some!

Sample Code:

[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
{
    SRCCOPY     = 0x00CC0020, /* dest = source*/
    SRCPAINT    = 0x00EE0086, /* dest = source OR dest*/
    SRCAND      = 0x008800C6, /* dest = source AND dest*/
    SRCINVERT   = 0x00660046, /* dest = source XOR dest*/
    SRCERASE    = 0x00440328, /* dest = source AND (NOT dest )*/
    NOTSRCCOPY  = 0x00330008, /* dest = (NOT source)*/
    NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
    MERGECOPY   = 0x00C000CA, /* dest = (source AND pattern)*/
    MERGEPAINT  = 0x00BB0226, /* dest = (NOT source) OR dest*/
    PATCOPY     = 0x00F00021, /* dest = pattern*/
    PATPAINT    = 0x00FB0A09, /* dest = DPSnoo*/
    PATINVERT   = 0x005A0049, /* dest = pattern XOR dest*/
    DSTINVERT   = 0x00550009, /* dest = (NOT dest)*/
    BLACKNESS   = 0x00000042, /* dest = BLACK*/
    WHITENESS   = 0x00FF0062, /* dest = WHITE*/
};

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