Reputation: 928
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
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:
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:
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
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