C# – semi-transparent image on transparent form

cnettransparency

i need to have semi-transparent image (by using alpha blending) drawn on fully transparent form – it means that image will be drawn over form transparent content.

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

This is 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);
    }
}

Best Answer

I guess since a Form doesn't support a transparent background color this may well be impossible. That way the form's background will always have a color, even when drawing an image with alpha channel on it.

Here's a similar question:

Related Topic