How to fix blurry Windows Forms Windows in high-dpi settings

If you have just opened one of your previous applications in your brand new computer with a very high-dpi monitor, perhaps you will find out that your interface that previously worked perfectly is now utterly broken and/or blurry.

This might be the case, for example, if you just tried to open an old Windows.Forms application in your brand new Surface Pro computer.

winforms-dpi-off
Windows.Forms (WinForms) application in a high-dpi display. If you have your DPI set to 150%, your form might now look like this.

 

winforms-dpi-on
Same WinForms application after applying the fix detailed here.

 

How to fix it

  1. Go the the Forms designer, then select your Form (by clicking at its title bar)
  2. Press F4 to open the Properties window, then locate the AutoScaleMode property
  3. Change it from Font (default) to Dpi.

winforms-dpi-turning

Now, go to Program.cs (or the file where your Main method is located) and change it to look like

Save and compile. Now your form should look crispy again.

I encountered this problem while opening and editing Accord.NET sample applications in Visual Studio in a Surface 3 Pro.

Related resources

Hidden Conditional Random Fields

If you are attempting to solve a sequence classification problem, perhaps you might be interested on learning about Hidden Conditional Random Fields (HCRFs). Those are the discriminant counterpart of  classifiers based on a set of hidden Markov models (HMMs).

Hidden Conditional Random Field
Credit: theaucitron, wheat fields.

Hidden what?

Hidden Markov models are simple models that can be created to recognize whether a sequence of observations is similar to the previous sequences that the model has seen before. However, if we create one HMM after each type of sequence that we are trying to distinguish, and them individually ask each model whether it recognizes the given sequence, we have just created a hidden Markov model classifier.

However, we might have a slightly better way of classifying those sequences. This method for creating a classifier (i.e. creating individual models to model each sequence type, then asking which model how strongly it recognizes a new sequence) is known as generative learning. But we could also have created a model from the ground-up that was just focused on distinguishing between sequence types without modeling them first. This would be known as discriminative learning.

And as mentioned in the tagline for this article, HCRFs are the discriminative doppelganger of the HMM classifier. Let’s see then how we can use them.

Creating HCRFs sequence classifiers in C#/.NET

If you would like to explore them in your projects, the Accord.NET Framework provides Hidden Markov Models, Hidden Markov Model Classifiers, Conditional Random Fields and Hidden Conditional Random Fields.

Continue reading →

Version invariant deserialization in .NET

If you have serialized an object using a previous version of a library or program, after you try to deserialize this object again you might encounter the following exception:

{“Exception has been thrown by the target of an invocation.”}

The inner exception might read:

[System.IO.FileLoadException] = {“Could not load file or assembly ‘Accord.Math, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)”:”Accord.Math, Version=2.13.1.0, Culture=neutral, PublicKeyToken=fa1a88e29555ccf7″}

In this case, it is very likely that those exceptions are occurring because the .NET run-time is looking for an assembly with the specific version indicated above. Even if you have new assemblies with exact the same name and exact public key token, the .NET might still refuse to deserialize it.

In order to get around this, put the following static class into your application:

Now, go back where you were using your deserializer and getting that exception, and instead of calling formatter.Deserialize, call formatter.DeserializeAnyVersion:

 

Deserialization now might work as expected; but please keep in mind that we might be loosing some security here. However, this might be a concern only if your application is dynamically loading assemblies at run-time.

Here are some resources discussing the problem:

Such extension method will also be included in the Accord.NET Framework.