3

In our C# clickonce application we want to get the user's screen size (width, height). At first everything worked fine with

int width = Screen.FromControl(this).Bounds.Width;
int height= Screen.FromControl(this).Bounds.Height;

Later on I noticed that on our Windows 8.1 laptop the values returned are 1.25 times less than the real dimensions of the screen. That's when I noticed we have a DPI problem with certain Windows versions on high resolution screens.

I know that moving on to WPF would be a good option, but we are in need of a quick and dirty fix. After searching by Google I found several approaches as described here and I decided to go with this solution. After all, even the MSDN Blog entries suggest to add the following snippet to the app.manifest:

<asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
        <dpiAware>true</dpiAware>
    </asmv3:windowsSettings>
</asmv3:application>

Unfortunately I do not understand how and where to add this code. My manifest (automatically generated by Visual Studio) looks like this:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="asInvoker" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>   
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
        </application>
    </compatibility> 
</asmv1:assembly>

Note that I have removed commented parts to make the app manifest look smaller here. I have tried adding the snippet from above before </asm1:assembly> on the last line. Whenever I tried this, I get an exception before my application starts:

An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll.
Additional information: Value cannot be null.

Oddly, someone else on stackoverflow.com suggested to uncheck Enable clickonce security settings in the app settings. This made the application start again and it worked on my laptop as expected: The app was upscaled and the text sharp. Also, I was able to get the correct screen size in pixels through my code.

Of course disabling the security settings is not an option if you want to deploy an application. So how can I fix this problem in order to get what I want? What am I missing? I am totally lost at this point.


Trying to start fresh:

  • Opened Visual Studio (2013 Pro)
  • New Windows Forms application called "Test"
  • Enabled "ClickOnce security settings"
  • Edited the manifest file again as suggest on other posts like here
  • Clicked on Start (or Build)

The following error appears. I don't know how to get more information about the error, becuase there is none...

enter image description here

enter image description here

Community
  • 1
  • 1
andreas
  • 7,844
  • 9
  • 51
  • 72
  • It seems you are declaring the manifest correctly. So you're left with the exception. But you haven't provided a complete code example nor have you provide complete information about the exception. Please show the complete stack trace for the exception, and provide [a good, complete code example](https://stackoverflow.com/help/mcve). – Peter Duniho Jan 14 '15 at 03:34
  • @PeterDuniho there was no complete stack trace, or at least I could not find it. But I added some information, I could even reproduce the problem by starting a new sample project without any edits. – andreas Jan 14 '15 at 22:23

1 Answers1

-1

Maybe it is too late for answer, but according to the answer of the same problem

Project Options > Debug > Enable the Visual Studio hosting process(disable it run app later enable it again)

momvart
  • 1,737
  • 1
  • 20
  • 32