示例#1
0
        private void FadeImages()
        {
            using (var sourceGraphics = Graphics.FromImage(_screenBuffer[_lastScreenBuffer]))
                using (var destinationGraphics = Graphics.FromImage(_screenBuffer[_activeScreenBuffer]))
                {
                    IntPtr sourceDC              = IntPtr.Zero;
                    IntPtr sourceBitmapHandle    = IntPtr.Zero;
                    IntPtr oldSourceObjectHandle = IntPtr.Zero;

                    IntPtr destinationDC              = IntPtr.Zero;
                    IntPtr destinationBitmapHandle    = IntPtr.Zero;
                    IntPtr oldDestinationObjectHandle = IntPtr.Zero;

                    try
                    {
                        sourceDC           = sourceGraphics.GetHdc();
                        sourceBitmapHandle = _screenBuffer[_lastScreenBuffer].GetHbitmap();

                        oldSourceObjectHandle = Gdi32.SelectObject(sourceDC, sourceBitmapHandle);
                        if (oldSourceObjectHandle == IntPtr.Zero)
                        {
                            throw new Win32Exception();
                        }

                        destinationDC           = destinationGraphics.GetHdc();
                        destinationBitmapHandle = _screenBuffer[_activeScreenBuffer].GetHbitmap();

                        oldDestinationObjectHandle = Gdi32.SelectObject(destinationDC, destinationBitmapHandle);
                        if (oldDestinationObjectHandle == IntPtr.Zero)
                        {
                            throw new Win32Exception();
                        }

                        var constantAlphaBlend = new Gdi32.BlendFunction
                        {
                            BlendOp             = Gdi32.AC_SRC_OVER,
                            SourceConstantAlpha = 0x99,
                        };

                        if (!Gdi32.AlphaBlend(destinationDC, 0, 0, _screenBuffer[_activeScreenBuffer].Width, _screenBuffer[_activeScreenBuffer].Height,
                                              sourceDC, 0, 0, _screenBuffer[_lastScreenBuffer].Width, _screenBuffer[_lastScreenBuffer].Height,
                                              constantAlphaBlend))
                        {
                            throw new Win32Exception();
                        }
                    }
                    finally
                    {
                        if (oldDestinationObjectHandle != IntPtr.Zero)
                        {
                            Gdi32.SelectObject(destinationDC, oldDestinationObjectHandle);
                        }
                        if (destinationBitmapHandle != IntPtr.Zero)
                        {
                            Gdi32.DeleteObject(destinationBitmapHandle);
                        }
                        if (destinationDC != IntPtr.Zero)
                        {
                            destinationGraphics.ReleaseHdc(destinationDC);
                        }

                        if (oldSourceObjectHandle != IntPtr.Zero)
                        {
                            Gdi32.SelectObject(sourceDC, oldSourceObjectHandle);
                        }
                        if (sourceBitmapHandle != IntPtr.Zero)
                        {
                            Gdi32.DeleteObject(sourceBitmapHandle);
                        }
                        if (sourceDC != IntPtr.Zero)
                        {
                            sourceGraphics.ReleaseHdc(sourceDC);
                        }
                    }
                }
        }