예제 #1
0
        // Sets the current bitmap
        public void SelectBitmap(Bitmap bitmap)
        {
            // Does this bitmap contain an alpha channel?
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            // Get device contexts
            IntPtr screenDc   = APIHelp.GetDC(IntPtr.Zero);
            IntPtr memDc      = APIHelp.CreateCompatibleDC(screenDc);
            IntPtr hBitmap    = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try
            {
                // Get handle to the new bitmap and select it into the current device context
                hBitmap    = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);

                // Set parameters for layered window update
                APIHelp.Size          newSize        = new APIHelp.Size(bitmap.Width, bitmap.Height);   // Size window to match bitmap
                APIHelp.Point         sourceLocation = new APIHelp.Point(0, 0);
                APIHelp.Point         newLocation    = new APIHelp.Point(this.Left, this.Top);          // Same as this window
                APIHelp.BLENDFUNCTION blend          = new APIHelp.BLENDFUNCTION();
                blend.BlendOp             = APIHelp.AC_SRC_OVER;                                        // Only works with a 32bpp bitmap
                blend.BlendFlags          = 0;                                                          // Always 0
                blend.SourceConstantAlpha = 255;                                                        // Set to 255 for per-pixel alpha values
                blend.AlphaFormat         = APIHelp.AC_SRC_ALPHA;                                       // Only works when the bitmap contains an alpha channel

                // Update the window
                APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize,
                                            memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
            }
            finally
            {
                // Release device context
                APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    APIHelp.SelectObject(memDc, hOldBitmap);
                    APIHelp.DeleteObject(hBitmap);                                                                                              // Remove bitmap resources
                }
                APIHelp.DeleteDC(memDc);
            }
        }
예제 #2
0
        // Sets the current bitmap
        public void SelectBitmap(Bitmap bitmap)
        {
            // Does this bitmap contain an alpha channel?
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            // Get device contexts
            IntPtr screenDc = APIHelp.GetDC(IntPtr.Zero);
            IntPtr memDc = APIHelp.CreateCompatibleDC(screenDc);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try
            {
                // Get handle to the new bitmap and select it into the current device context
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);

                // Set parameters for layered window update
                APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height);	// Size window to match bitmap
                APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
                APIHelp.Point newLocation = new APIHelp.Point(this.Left, this.Top);		// Same as this window
                APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION();
                blend.BlendOp             = APIHelp.AC_SRC_OVER;						// Only works with a 32bpp bitmap
                blend.BlendFlags          = 0;											// Always 0
                blend.SourceConstantAlpha = 255;										// Set to 255 for per-pixel alpha values
                blend.AlphaFormat         = APIHelp.AC_SRC_ALPHA;						// Only works when the bitmap contains an alpha channel

                // Update the window
                APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize,
                    memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
            }
            finally
            {
                // Release device context
                APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    APIHelp.SelectObject(memDc, hOldBitmap);
                    APIHelp.DeleteObject(hBitmap);										// Remove bitmap resources
                }
                APIHelp.DeleteDC(memDc);
            }
        }