Translucent image on a transparent form

I need to have a semi-transparent image (using alpha blending) drawn on a fully transparent form - this means the image will be drawn on top of the transparent content.

Currently, the image is always drawn over the background color of the window, even if the window itself is transparent.

This is the current state, thanks for any help.

public Form1()
{
    InitializeComponent();

    SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

    MakeTransparent();
}

private void MakeTransparent()
{
    NativeMethods.SetLayeredWindowAttributes(Handle, COLORREF.FromColor(BackColor), 255, Constants.ULW_ALPHA | Constants.ULW_COLORKEY);
}

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp =  base.CreateParams;
        cp.Style |= (Constants.WS_EX_LAYERED | Constants.WS_EX_TOOLWINDOW);
        return cp;
    }
}

private void OnPaint(object sender, PaintEventArgs e)
{
    using (Bitmap bitmap = new Bitmap("c:\\semi-transparent.png"))
    {
        e.Graphics.DrawImage(bitmap, 0, 0);
    }
}

      

+2


source to share


2 answers


I think since the form does not support transparent background color this may not be possible. This way, the background of the form will always have a color, even when drawing an image with an alpha channel on it.

Here's a similar question:



+1


source


OK, thanks for answer.

I was actually able to do this using the UpdateLayeredWindow function, but I had to always update the bitmap of the entire window, even if I really only needed to redraw a small portion of the window.



Capturing the screen content and drawing it below the image is not really a solution because I need my window to be movable.

+1


source







All Articles