Monday, June 21, 2010

Troubleshoot Grapics Rendering issues in WPF

WPF renders everything using either using Hardware or Software or partial to both. If your computer is capable of rendering graphics using Hardware acceleration it goes with it to produce superior graphics and also getting input for Graphics Storage. Producing graphics using hardware generally be faster and smoother as it can register the memory of graphics and hence the application memory remains unaltered. Using hardware rendering technique is always better for an application, but while sometimes you see some weird issue regarding the hardware acceleration. Lets discuss about the issue.

A few days back, I found my application cannot display few text in some computers. I have a Tablet which runs Windows XP professional, and when I run my application there, text which have Gradient in it, doesn't display at all. I was really surprised to see it, but I found that sometimes when the graphics drivers are not installed properly in the machine, the drivers cannot produce gradient graphics properly, and hence there will be an issue with Text Rendering.

To fix the issue there are few techniques that you can follow. The first and most likely troubleshoot might be to install latest drivers for your Graphics card. But this makes you to learn more about your existing graphics drivers and its capabilities. Sometimes it might require that your own graphics driver does not support the gradient rendering. Then this will not be possible for you to change your graphics driver to solve this issue.

So I thought a way to the users to disable the Hardware acceleration.
To Disable Hardware Acceleration just follow the steps :



1. Go to Windows Desktop, Right click on blank area and select properties.

2. A new popup will open(as in fig 1).
3. Select Settings - > Advanced button.

4. On the Advanced dialog, please choose TroubleShoot, and drag the scroller that says Hardware Acceleration to None.


And then when I run your application, I think your issue must fix.

If you are a doing application in .NET, please read on.

Now this is not a solution for my problem. I cannot tell all of my people to do this crap to each of those computer where it creates issues. Thus I tried to programmatic solution to handle these myself.

WPF 4.0 comes with a property called

RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;

But my application is built using WPF 3.5.

To disable hardware acceleration:
[HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics]
"DisableHWAcceleration"=dword:00000001

To enable hardware acceleration:
[HKEY_CURRENT_USER\Software\Microsoft\Avalon.Graphics]
"DisableHWAcceleration"=dword:00000000


You can read more from here.

But this would also affect other applications which are using Graphics acceleration.
Knowledge base article like http://support.microsoft.com/kb/963021 comes very helpful when I find issues with these kind of scenarios.

Windows XP also has few problems with layered display. For that, we need
http://support.microsoft.com/kb/937106/en-us
which is said to improve performance of WPF applications. But with all these there is no surety to have my application running properly./ So finally I have fixed by running only my application in Software Mode using the following code :

if (SoftwareRenderingNeeded)
            {

                HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
                HwndTarget hwndTarget = hwndSource.CompositionTarget;
                
                // this is the new WPF API to force render mode.
                hwndTarget.RenderMode = RenderMode.SoftwareOnly;

              }

So I guess the problem is solved for now, until I find another problem in this regard.
Please respond if you have any better solution to this. I would love to see.

Thanks for reading my post.

No comments:

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!