Nevron Forum

Convert value of NLength to different measurement unit

https://www.nevron.com/Forum/Topic8375.aspx

By Kevin Harrison 1 - Thursday, December 19, 2013

Is it possible to easily an NLength in one measurement unit into a different measurement unit?

e.g. I have a length in NGraphicsUnit.Pixel which I want to convert to NRelativeUnit.ParentPercentage

Thanks

Kevin

By Nevron Support - Friday, December 20, 2013

Hi Kevin,

Yes there is an easy way to convert values from one measurement unit to another. The following code shows how to create and initialize a measurement unit converter class that can be used to perform this conversion:

NMeasurementUnitConverter converter = new NMeasurementUnitConverter(NResolution.ScreenResolution);

NSizeF rootSize = new NSizeF(200, 200);
NSizeF parentSize = new NSizeF(100, 100);
converter.InitializeRelativeMembers(rootSize, parentSize);

float parentPercentage = converter.ConvertX(NGraphicsUnit.Pixel, NRelativeUnit.ParentPercentage, 10);

Note that in general the conversion is defined when you specify x/y resolution, x/y parent size (used by the ParentPercentage unit) and x/y root size (used by the RootPercentageUnit). Hope this helps - let us know if you have any questions...