public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj)
 {
     IntPtr ret = _SelectObjectSafeHBITMAP(hdc, hgdiobj);
     if (ret == IntPtr.Zero)
     {
         HRESULT.ThrowLastError();
     }
     return ret;
 }
 private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
        public void Show()
        {
            _VerifyMutability();

            Stream imageStream = null;
            try
            {
                // Try to use the filepath first.  If it's not provided or not available, use the embedded resource.
                if (!string.IsNullOrEmpty(ImageFileName) && File.Exists(ImageFileName))
                {
                    try
                    {
                        imageStream = new FileStream(ImageFileName, FileMode.Open);
                    }
                    catch (IOException) { }
                }

                if (imageStream == null)
                {
                    imageStream = _resourceManager.GetStream(ResourceName, CultureInfo.CurrentUICulture);
                    if (imageStream == null)
                    {
                        throw new IOException("The resource could not be found.");
                    }
                }

                Size bitmapSize;
                _hBitmap = _CreateHBITMAPFromImageStream(imageStream, out bitmapSize);

                Point location = new Point(
                    (NativeMethods.GetSystemMetrics(SM.CXSCREEN) - bitmapSize.Width) / 2,
                    (NativeMethods.GetSystemMetrics(SM.CYSCREEN) - bitmapSize.Height) / 2);

                // Pass a null WndProc.  Let the MessageWindow use DefWindowProc.
                _hwndWrapper = new MessageWindow(
                    CS.HREDRAW | CS.VREDRAW,
                    WS.POPUP | WS.VISIBLE,
                    WS_EX.WINDOWEDGE | WS_EX.TOOLWINDOW | WS_EX.LAYERED | (IsTopMost ? WS_EX.TOPMOST : 0),
                    new Rect(location, bitmapSize),
                    "Splash Screen",
                    null);

                byte opacity = (byte)(FadeInDuration > TimeSpan.Zero ? 0 : 255);

                using (SafeDC hScreenDC = SafeDC.GetDesktop())
                {
                    using (SafeDC memDC = SafeDC.CreateCompatibleDC(hScreenDC))
                    {
                        IntPtr hOldBitmap = NativeMethods.SelectObject(memDC, _hBitmap);

                        RECT hwndRect = NativeMethods.GetWindowRect(_hwndWrapper.Handle);

                        POINT hwndPos = hwndRect.Position;
                        SIZE hwndSize = hwndRect.Size;
                        POINT origin = new POINT();
                        BLENDFUNCTION bf = _BaseBlendFunction;
                        bf.SourceConstantAlpha = opacity;

                        NativeMethods.UpdateLayeredWindow(_hwndWrapper.Handle, hScreenDC, ref hwndPos, ref hwndSize, memDC, ref origin, 0, ref bf, ULW.ALPHA);
                        NativeMethods.SelectObject(memDC, hOldBitmap);
                    }
                }

                if (CloseOnMainWindowCreation)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke(
                        DispatcherPriority.Loaded,
                        (DispatcherOperationCallback)delegate(object splashObj)
                        {
                            var splashScreen = (SplashScreen)splashObj;
                            if (!splashScreen._isClosed)
                            {
                                splashScreen.Close();
                            }
                            return null;
                        },
                        this);
                }

                _dispatcher = Dispatcher.CurrentDispatcher;
                if (FadeInDuration > TimeSpan.Zero)
                {
                    _fadeInEnd = DateTime.UtcNow + FadeInDuration;
                    _dt = new DispatcherTimer(FadeInDuration, DispatcherPriority.Normal, _FadeInTick, _dispatcher);
                    _dt.Start();
                }
            }
            finally
            {
                Utility.SafeDispose(ref imageStream);
            }
        }