0

How do you create the transparency effects that you see in windows 10? Something like this:

The blur effect can also be seen on Edge browser

I have no clue how to approach this in c#. Logically thinking I would take a snapshot of the desktop every time the form comes into focus. Then blur it and place it at 0, 0(screen to client coordinates). That doesn't seem very effective. Any help? Again. not an experienced C# programmer, so a detailed explanation would be much appreciated

Edit: I saw some the answers referring me to a page for alpha blending. This is not what I am looking for. I wanted to know how to create the blur that you see in the image, the rest I can figure out at my own pace

Vaf Daf
  • 57
  • 1
  • 6
  • 4
    If you want advanced graphics, WinForms won't help you at all. You will need to look at either WPF or UWP – Camilo Terevinto Jul 16 '18 at 22:08
  • 3
    It is a per-pixel alpha blending effect. Actually implemented by DirectComposition, two decades removed from [winforms]. It is [not impossible to use](https://www.codeproject.com/Articles/1822/Per-Pixel-Alpha-Blend-in-C) in a Winforms app, but you can't use most of the controls in the toolbox anymore. They use 24bpp GDI calls to render. WPF uses a rendering model that is compatible with it. – Hans Passant Jul 16 '18 at 22:09
  • A little googling and I found that on SO: https://stackoverflow.com/questions/4096609/c-sharp-how-to-make-a-windows-7-aero-winform-blured-glass – nabuchodonossor Jul 17 '18 at 05:41
  • I have posted some related code a few months ago. [Borderless Form Dropshadow](https://stackoverflow.com/questions/49395005/borderless-form-dropshadow?answertab=active#tab-top). It was about adding the Aero style DropShadow to a borderless WinForm. But I left there by mistake a lot of other DWM Api related methods. Those include implementations for `DwmExtendFrameIntoClientArea()` (to create a Sheet Of Glass) and `DwmEnableBlurBehindWindow()`. Maybe it can be of some use for you. – Jimi Jul 17 '18 at 21:25

3 Answers3

3

Though all of the comments and the answers say it is not possible for WinForms, it definitely works also for WinForms (as SetWindowCompositionAttribute can be called on a Win32 window handle):

internal enum AccentState
{
    ACCENT_DISABLED = 0,
    ACCENT_ENABLE_GRADIENT = 1,
    ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
    ACCENT_ENABLE_BLURBEHIND = 3,
    ACCENT_INVALID_STATE = 4
}

internal enum WindowCompositionAttribute
{
    WCA_ACCENT_POLICY = 19
}

[StructLayout(LayoutKind.Sequential)]
internal struct AccentPolicy
{
    public AccentState AccentState;
    public int AccentFlags;
    public int GradientColor;
    public int AnimationId;
}

[StructLayout(LayoutKind.Sequential)]
internal struct WindowCompositionAttributeData
{
    public WindowCompositionAttribute Attribute;
    public IntPtr Data;
    public int SizeOfData;
}

internal static class User32
{
    [DllImport("user32.dll")]
    internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
}

And then in your Form constructor:

public Form1()
{
    InitializeComponent();
    BackColor = Color.Black; // looks really bad with the default back color

    var accent = new AccentPolicy { AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND };

    var accentStructSize = Marshal.SizeOf(accent);

    var accentPtr = Marshal.AllocHGlobal(accentStructSize);
    Marshal.StructureToPtr(accent, accentPtr, false);

    var data = new WindowCompositionAttributeData
    {
        Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
        SizeOfData = accentStructSize,
        Data = accentPtr
    }

    User32.SetWindowCompositionAttribute(Handle, ref data);
    Marshal.FreeHGlobal(accentPtr);
}

Result:

Windows Form with Windows 10 transparency blur effect

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • It is workig. Question: Why are the controls blurry too? – IR_IR Sep 30 '22 at 13:14
  • 1
    @IR_IR: A panel, label or other control without a custom back color will inherit the blurry background, as well as the default `ControlText` foreground color. But if you set a specific fore/back color, then it will break the 'blurriness'. – György Kőszeg Oct 01 '22 at 15:31
  • Controls of the form are affected even if I use non-default fore/back color!!! Can you please take a look here? https://stackoverflow.com/q/75040397/6336312 – Simos Sigma Jan 07 '23 at 15:13
2

Windows Forms doesn't support AcrylicBrush so far. Only UWP support this.

But you have a Win32 API SetWindowCompositionAttribute to simulate this behavior.


The SetWindowCompositionAttribute API

By calling the Windows internal API SetWindowCompositionAttribute, you can get a lightly blurred transparent Window but this transparency is much less than the AcyclicBrush.
The image from my post

How to implement it

Calling SetWindowCompositionAttribute API is not very easy, so I've written a wrapper class for easier usage. But it's for WPF only.

I've written two posts talking about this:

Other options

It's recommended to use AcrylicBrush using UWP and you can read Microsoft's documents Acrylic material - UWP app developer | Microsoft Docs for more details about it.

walterlv
  • 2,366
  • 13
  • 73
0

You can get that by using some algorithms

I don't know will it work or not. When I see your post I just think and got this concept.

  1. Get entire windows desktop wallpaper image by using copyscreen method

  2. Draw that image into a new bitmap where bitmap width = screen resolution.width and bitmap height = screen resolution.height eg: Bitmap bm = new Bitmap (1920, 1080)

  3. Learn how to blur a bitmap image in c#. There is many of blogs teaching how to blur a bitmap programmatically.

  4. Make blur of captured desktop wallpaper image

  5. Put a picture of inside the form and draw the blurred bitmap into picturebox

  6. You can make FormBorderStyle.None to get rid of old borders

  7. You should keep picturebox picture style = normal in order to get full blurred image

tripleee
  • 175,061
  • 34
  • 275
  • 318
ARUN V
  • 33
  • 1
  • 6