Пример #1
0
        public static Color GetPixelColor(int x, int y)
        {
            IntPtr hdc   = NativeMethods.GetDC(IntPtr.Zero);
            uint   pixel = NativeMethods.GetPixel(hdc, x, y);

            NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
            return(Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16));
        }
Пример #2
0
        public void SelectBitmap(Bitmap bitmap, int opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            IntPtr screenDc   = NativeMethods.GetDC(IntPtr.Zero);
            IntPtr memDc      = NativeMethods.CreateCompatibleDC(screenDc);
            IntPtr hBitmap    = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try
            {
                hBitmap    = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = NativeMethods.SelectObject(memDc, hBitmap);

                SIZE          newSize        = new SIZE(bitmap.Width, bitmap.Height);
                POINT         sourceLocation = new POINT(0, 0);
                POINT         newLocation    = new POINT(this.Left, this.Top);
                BLENDFUNCTION blend          = new BLENDFUNCTION();
                blend.BlendOp             = NativeMethods.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = (byte)opacity;
                blend.AlphaFormat         = NativeMethods.AC_SRC_ALPHA;

                NativeMethods.UpdateLayeredWindow(this.Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, NativeMethods.ULW_ALPHA);
            }
            finally
            {
                NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(memDc, hOldBitmap);
                    NativeMethods.DeleteObject(hBitmap);
                }
                NativeMethods.DeleteDC(memDc);
            }
        }