Transparent Winform with image

I have an image (PNG file) that has an alpha channel that is set to 50% opaque. When I try to draw an image on a form for which the TransparencyKey is set to white and the backcolor is set to white, I expect the image to be drawn at 50%. However, it is blended with the backcolor shapes first, and as a result, it is completely opaque. Is there a way to get around this? I don't want to set the Opaque as some images in the form need to be semi-transparent and some need to be opaque.

+1


source to share


3 answers


I ended up using a layered window using the advanced WS_EX_LAYERED window style.



http://msdn.microsoft.com/en-us/library/ms997507.aspx

+1


source


I don't think you can. We have a splash screen where we did something like this, but we finished capturing the screen and set it as the background image of the form. Obviously this only works if the screen changes, the background of the form does not work and everything looks strange. If you find a better way to do this, I would like to know about it.

Here is the code to capture the screen, just set ScreenRect to the screen coordinates of the forms and call Process ():



using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace TourFactory.Core.Drawing
{
    public class CaptureScreenCommand
    {

        #region Initialization and Destruction

        public CaptureScreenCommand()
        {
        }

        #endregion

        #region Fields and Properties

        // BitBlt is a multipurpose function that takes a ROP (Raster OPeration) code
        // that controls exactly what it does. 0xCC0020 is the ROP code SRCCOPY, i.e.
        // do a simple copy from the source to the destination.
        private const int cRasterOp_SrcCopy = 0xCC0020; // 13369376;

        private Rectangle mScreenRect;
        /// <summary>
        /// Gets or sets the screen coordinates to capture.
        /// </summary>
        public Rectangle ScreenRect
        {
            get { return mScreenRect; }
            set { mScreenRect = value; }
        }

        #endregion

        #region Methods

        public Image Process()
        {
            // use the GDI call and create a DC to the whole display
            var dc1 = CreateDC("DISPLAY", null, 0, 0);
            var g1 = Graphics.FromHdc(dc1);

            // create a compatible bitmap the size of the form
            var bmp = new Bitmap(mScreenRect.Width, mScreenRect.Height, g1);
            var g2 = Graphics.FromImage(bmp);

            // Now go retrace our steps and get the device contexts for both the bitmap and the screen
            // Note: Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown
            // when you try to release the dcs
            dc1 = g1.GetHdc();
            var dc2 = g2.GetHdc();

            // Bit Blast the screen into the Bitmap
            BitBlt(dc2, 0, 0, mScreenRect.Width, mScreenRect.Height, dc1, mScreenRect.Left, mScreenRect.Top, cRasterOp_SrcCopy);

            // Remember to release the dc's, otherwise problems down the road
            g1.ReleaseHdc(dc1);
            g2.ReleaseHdc(dc2);

            // return bitmap
            return bmp;
        }

        #endregion

        #region gdi32.dll

        [DllImport("gdi32")]
        private static extern IntPtr CreateDC(string lpDriverName, string lpDeviceName, int lpOutput, int lpInitData);

        [DllImport("gdi32")]
        private static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int width, int height, IntPtr hdcSrc, int xSrc, int ySrc, int dwRop);

        #endregion

    }
}

      

0


source


Nice. Don't forget Vista has a Desktop Window Manager for creating semi-transparent windows (aka Areo) http://msdn.microsoft.com/en-us/magazine/cc163435.aspx

0


source







All Articles