Enable Software Rendering in WPF programmatically


From my previous post, I talked about troubleshooting WPF graphic issues. One of those options is to set a registry key to enable software rendering for WPF on that machine only. But that can be an intrusive setting and may not suit everyone’s needs.

There is a another option, which is to enable software rendering in code. If you are working on .NET 3.5 framework, there’s a way to set that up..unfortunately you have to set that up per window or per target control. But might be useful for those that want to selectively tweak rendering mode for each window.

private void ConfigureSoftwareRendering()
{
    if (shouldEnableSoftwareRendering)
    {
        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
        HwndTarget hwndTarget = hwndSource.CompositionTarget;
        hwndTarget.RenderMode = RenderMode.SoftwarewareOnly;
    }
}

If you’re developing in .NET 4 framework, then you have another option, which is to set up software rendering per process….like so.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
         if (shouldEnableSoftwareRendering)
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
    }
}

You might be wondering why we should use software rendering for WPF applications. It depends on the end-user hardware configuration which might have “WPF-unfriendly” drivers, or just no graphics card. In some situations software rendering might be faster than hardware rendering … hope this helps.

Share this post :
Advertisements
Posted in WPF. 4 Comments »

4 Responses to “Enable Software Rendering in WPF programmatically”

  1. sacha Says:

    Thank you very much for this post,
    It drastically improved the fluidity of my application, in just on line of code!

  2. Simon Says:

    Jolly helpful, thank you kindly.

  3. Coders Gotta Code » Blog Archive » WPF Graphics Band-Aid Says:

    […] If you’re using .Net 3.5 there are clunkier ways to do this on a per window or per control basis The Code Blitz blog shows you how here. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: