public static void SetBitmap(Bitmap bitmap, Form frm)
        {
            // retrieve current screen device context
            IntPtr screenDc = NativeMethods.GetDC(IntPtr.Zero);

            // create a memory device context compatible with the screen
            IntPtr compatibleMemoryDc = NativeMethods.CreateCompatibleDC(screenDc);

            IntPtr hgdiBitmap    = IntPtr.Zero;
            IntPtr hgdiOldBitmap = IntPtr.Zero;

            try
            {
                hgdiBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                // select a bitmap object into the memory device context created above
                hgdiOldBitmap = NativeMethods.SelectObject(compatibleMemoryDc, hgdiBitmap);

                // ++ prepare structures we need in order to call UpdateLayeredWindow ++
                NativeMethods.SizeStruct size = new NativeMethods.SizeStruct()
                {
                    X = bitmap.Width,
                    Y = bitmap.Height
                };

                NativeMethods.PointStruct sourcePoint = new NativeMethods.PointStruct();
                NativeMethods.PointStruct topPoint    = new NativeMethods.PointStruct()
                {
                    X = frm.Left,
                    Y = frm.Top
                };

                NativeMethods.BlendFunctionStruct blend = new NativeMethods.BlendFunctionStruct()
                {
                    BlendOp             = 255 /* opacity */,
                    BlendFlags          = 0x00 /* AC_SRC_OVER */,
                    AlphaFormat         = 0x01 /* AC_SRC_ALPHA */,
                    SourceConstantAlpha = byte.MaxValue
                };

                frm.Invoke((MethodInvoker) delegate
                {
                    NativeMethods.UpdateLayeredWindow(frm.Handle, screenDc, ref topPoint, ref size, compatibleMemoryDc,
                                                      ref sourcePoint, 0, ref blend, 2 /* ULW_ALPHA */);
                });
            }
            finally
            {
                if (screenDc != IntPtr.Zero)
                {
                    NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                }

                if (hgdiBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(compatibleMemoryDc, hgdiOldBitmap);
                    NativeMethods.DeleteObject(hgdiBitmap);
                }

                NativeMethods.DeleteDC(compatibleMemoryDc);
            }
        }
Example #2
0
            internal void SetBitmap(Bitmap bitmap, int alpha = 100)
            {
                IntPtr screenDc           = NativeMethods.GetDC(IntPtr.Zero);
                IntPtr compatibleMemoryDc = NativeMethods.CreateCompatibleDC(screenDc);
                IntPtr hgdiBitmap         = IntPtr.Zero;
                IntPtr hgdiOldBitmap      = IntPtr.Zero;

                try
                {
                    try
                    {
                        hgdiBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                    }
                    catch
                    { return; }
                    hgdiOldBitmap = NativeMethods.SelectObject(compatibleMemoryDc, hgdiBitmap);
                    NativeMethods.SizeStruct size = new NativeMethods.SizeStruct()
                    {
                        X = bitmap.Width,
                        Y = bitmap.Height
                    };
                    NativeMethods.PointStruct sourcePoint = new NativeMethods.PointStruct();
                    NativeMethods.PointStruct topPoint    = new NativeMethods.PointStruct()
                    {
                        X = Left,
                        Y = Top
                    };

                    NativeMethods.BlendFunctionStruct blend = new NativeMethods.BlendFunctionStruct()
                    {
                        BlendOp             = (byte)(255 * alpha / 100d) /* opacity */,
                        BlendFlags          = 0x00 /* AC_SRC_OVER */,
                        AlphaFormat         = 0x01 /* AC_SRC_ALPHA */,
                        SourceConstantAlpha = (byte)(255 * alpha / 100d)
                    };

                    try
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            NativeMethods.UpdateLayeredWindow(Handle, screenDc, ref topPoint, ref size, compatibleMemoryDc,
                                                              ref sourcePoint, 0, ref blend, 2 /* ULW_ALPHA */);
                        });
                    }
                    catch { }
                }
                finally
                {
                    if (screenDc != IntPtr.Zero)
                    {
                        NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                    }

                    if (hgdiBitmap != IntPtr.Zero)
                    {
                        NativeMethods.SelectObject(compatibleMemoryDc, hgdiOldBitmap);
                        NativeMethods.DeleteObject(hgdiBitmap);
                    }

                    NativeMethods.DeleteDC(compatibleMemoryDc);
                }
            }