private static extern bool _UpdateLayeredWindow(
     IntPtr hwnd, 
     SafeDC hdcDst, 
     [In] ref POINT pptDst, 
     [In] ref SIZE psize, 
     SafeDC hdcSrc, 
     [In] ref POINT pptSrc,
     int crKey,
     ref BLENDFUNCTION pblend,
     ULW dwFlags);
        public static SafeDC CreateCompatibleDC(SafeDC hdc)
        {
            SafeDC dc = null;
            try
            {
                IntPtr hPtr = IntPtr.Zero;
                if (hdc != null)
                {
                    hPtr = hdc.handle;
                }
                dc = NativeMethods.CreateCompatibleDC(hPtr);
                if (dc == null)
                {
                    HRESULT.ThrowLastError();
                }
            }
            finally
            {
                if (dc != null)
                {
                    dc._created = true;
                }
            }

            if (dc.IsInvalid)
            {
                dc.Dispose();
                throw new SystemException("Unable to create a device context from the specified device information.");
            }

            return dc;
        }
 private static extern IntPtr _SelectObject(SafeDC hdc, IntPtr hgdiobj);
 private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
 private static extern SafeHBITMAP _CreateDIBSection(SafeDC hdc, [In] ref BITMAPINFO bitmapInfo, int iUsage, [Out] out IntPtr ppvBits, IntPtr hSection, int dwOffset);
 public static void UpdateLayeredWindow(
     IntPtr hwnd,
     SafeDC hdcDst,
     ref POINT pptDst,
     ref SIZE psize,
     SafeDC hdcSrc,
     ref POINT pptSrc,
     int crKey,
     ref BLENDFUNCTION pblend,
     ULW dwFlags)
 {
     if (!_UpdateLayeredWindow(hwnd, hdcDst, ref pptDst, ref psize, hdcSrc, ref pptSrc, crKey, ref pblend, dwFlags))
     {
         HRESULT.ThrowLastError();
     }
 }
 public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj)
 {
     IntPtr ret = _SelectObjectSafeHBITMAP(hdc, hgdiobj);
     if (ret == IntPtr.Zero)
     {
         HRESULT.ThrowLastError();
     }
     return ret;
 }
 public static extern int GetDeviceCaps(SafeDC hdc, DeviceCap nIndex);
        public static SafeHBITMAP CreateDIBSection(SafeDC hdc, ref BITMAPINFO bitmapInfo, out IntPtr ppvBits, IntPtr hSection, int dwOffset)
        {
            const int DIB_RGB_COLORS = 0;
            SafeHBITMAP hBitmap = null;
            if (hdc == null)
            {
                hBitmap = _CreateDIBSectionIntPtr(IntPtr.Zero, ref bitmapInfo, DIB_RGB_COLORS, out ppvBits, hSection, dwOffset);
            }
            else
            {
                hBitmap = _CreateDIBSection(hdc, ref bitmapInfo, DIB_RGB_COLORS, out ppvBits, hSection, dwOffset);
            }

            if (hBitmap.IsInvalid)
            {
                HRESULT.ThrowLastError();
            }

            return hBitmap;
        }