1

I'm trying to use a background image with rounded corners against a 'lime' background colour with the transparencykey set to 'lime', it mostly has the desired effect of just showing all the form controls on the background image but the lime background is not removed from the transparency key for 1px around the rounded edges:

1px ring left on all 4 corners

Does anyone know how i can prevent this happening?

Dan Hall
  • 1,474
  • 2
  • 18
  • 43
  • 1
    Well, yeah, that's not lime. Anti-aliasing needs to know the *real* background colour to work properly, and `TransparencyKey` only works with a single colour. – Luaan Jul 21 '16 at 07:42
  • 1
    Your graphics editor used the alpha channel in a pixel to make the image look decent against *any* background color. Anti-aliasing otherwise can only work well when the background is known so it can smoothly interpolate from the fore to background colors. But of course it can't work when you use a transparency key, it isn't Lime anymore when the alpha is applied. Per-pixel alpha blending cannot work in Winforms, no more paint events = dead controls. You'll have to fix the image. And wonder how it could *ever* possibly work correctly when you don't have a known back color. It can't.. – Hans Passant Jul 21 '16 at 08:12

1 Answers1

3

TransparencyKey works with a single color. When you use anti-aliasing to draw a round corner, the line color will be mixed with the background color so the color which you see is not exactly the Lime, it's a mixture of Lime and Gray to make the round corner more smooth. So when you set TransparencyKey to Lime that color(s) will not be removed.

To have a high-quality alpha blend round corner, consider using Layered Windows. Create a bitmap with transparent background containing smooth drawing of round rectangle then using the technique which you can find in below post, make your shaped form:

Using a layered window can significantly improve performance and visual effects for a window that has a complex shape, animates its shape, or wishes to use alpha blending effects. The system automatically composes and repaints layered windows and the windows of underlying applications. As a result, layered windows are rendered smoothly, without the flickering typical of complex window regions. In addition, layered windows can be partially translucent, that is, alpha-blended.

Important Note

The technique which described above is useful for creating a simple splash screen, but when you want to place some control on your form, you need to use a workaround.

Additional to your main form, you can create another window with the WS_EX_LAYERED style and use that to show the alpha blended background image. Then we just need to make sure that this window is positioned behind the main form. Yes, this is not such easy, but here is a library which makes the task really simple. Thanks to Anthony Mushrow for the idea and implementation:

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks Reza,I've given this a go with the class PerPixelAlphaForm.cs, my main form with all form elements on it (textboxes etc) inherits the class and then calls the SelectBitmap method with the background image as a parameter (MyMainForm.SelectBitmap(Properties.Resources.rounded1);). Unfortunately it makes the entire form completely white but despite this all controls can still be used, any idea why that is? – Dan Hall Jul 21 '16 at 10:00
  • Yes, I know, this is why I posted the **Note** part. `PerPixelAlphaForm` is really useful for creating splash screens. But if you want to have some controls on the form follow the idea of positioning your transparent-background mainform over an alpha blended form. – Reza Aghaei Jul 21 '16 at 10:01
  • Aha, thank you, didn't see the update...I'll give this a go. – Dan Hall Jul 21 '16 at 10:16
  • You're welcome. surely the solutions for the problem will not be perfect, because windows forms doesn't have support for such requirement as mentioned by @HansPassant in the comments. But the idea would be useful for some cases as described in the linked article. – Reza Aghaei Jul 21 '16 at 10:20
  • All working fine for what i need, the requirement was to update a C# project but couldn't use WPF, needed a popup window to add some user data with some nice border styling, this works perfectly for what i need here. Thanks again. – Dan Hall Jul 21 '16 at 10:54