2
votes

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);
    }
}
2
Try disabling the double buffer.Ruud

2 Answers

1
votes

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:

1
votes

Well, thanks for the answer.

I actually was able to do this by using UpdateLayeredWindow function but i had to always update whole window bitmap even if i really needed to redraw just small portion of window.

Capturing screen content and draw it under image is not really a solution because i need my window to be moveable.