name1ess0ne
name1ess0ne

Reputation: 928

How to fill desktop with transparent color?

I use this code to fill desktop with color.

IntPtr desktop = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktop);
g.FillRectangle(new SolidBrush(Color.FromArgb(60, 255, 0, 0), Screen.PrimaryScreen.Bounds);      
ReleaseDC(IntPtr.Zero, desktop);

This works fine, but very slow. If I fill desktop with nontransparent color Color.FromArgb(255, 255, 0, 0) it works very fast. How I can increase speed of transparent color drawing?

Upvotes: 0

Views: 359

Answers (2)

William Lawn Stewart
William Lawn Stewart

Reputation: 1205

Using GDI+ to do anything involving full transparency will always be really slow. This is because transparency involves a lot of multiplication and division to blend the pixel colours, while drawing opaque colours is simply a matter of overwriting the old pixels. Its a lot more processor intensive.

Joe White's method is a good alternative. Having a maximised form with a backcolor and low opacity would work just fine, and be a lot faster. Keep in mind though, that using a form comes with a few other things to worry about:

  • The form will block clicks to desktop icons
  • The form could be minimised by the user, removing the red tinge - even if its not in the taskbar, windows key + D will minimise it (although of course, its just a matter of writing code to get the form displaying again)
  • The form might end up over the top of other forms, giving them a red tinge (you can set forms to be below all other forms though, which would fix this issue)

Its not as slow, but there are other things to worry about when using a form instead of painting directly to the desktop.

It really depends on why you want to tint the desktop red - Depending on what you're trying to achieve, there might be other even easier methods:

  • You could tint the desktop wallpaper red - It won't affect the icons, but it won't have any performance issues
  • If you don't care about desktop interaction you could take a screenshot, tint it red, then display it in a maximised form.
  • Your current method would be just fine if you don't think you'll need to redraw it back on very often

Also, don't forget that some people have multiple monitor setups. You'll need to create one form per monitor if you want to cover them all.

Upvotes: 1

Joe White
Joe White

Reputation: 97808

GDI+ (which is what's used by System.Drawing) is implemented all in software, and isn't known for its speed.

You might be better off solving your problem a different way. Try making a Form with BorderStyle=None, sizing it to fill the entire screen, and set its BackColor to red and its Opacity to 0.25 (close approximation of your alpha=60).

Upvotes: 2

Related Questions