DpiHelper Class
A helper class containing various procedures related to DPI-awareness.
public static class DpiHelper
- Inheritance:
- object object
Remarks
Scale factors are consistently represented as SizeF
even when the value to be scaled
does not have related x- and y-axis properties as this helps avoid integer values (like a device DPI)
being accidentally passed as a float-based scale factor. When a scale value does not have related
x- and y-axis properties, the SizeF.Width
will define the scale to be applied.
Properties
IsDotNet6OrGreater
Tests if the current run-time environment is .NET 6 or greater.
public static bool IsDotNet6OrGreater { get; }
Property Value
- bool:
true
if the run-time environment is .NET 6 or greater; otherwise,false
if the run-time environment is .NET Core 3.1 or .NET 5.
IsPerMonitorScalingRequired
Tests if per-monitor application scaling may be required based on DPI awareness and the current screen configuration.
public static bool IsPerMonitorScalingRequired { get; }
Property Value
- bool:
true
if per-monitor scaling my be required; otherwise,false
.
See Also
IsPerMonitorV2Aware
Tests if the currently executing thread supports per-monitor V2 DPI awareness.
public static bool IsPerMonitorV2Aware { get; }
Property Value
- bool:
true
if the current context is per-monitor V2 aware; otherwise,false
.
See Also
Methods
AutoScaleFont(Font, SizeF)
Automatically rescales a font if the scale factor for system DPI is different than scale factor where the font will be used.
public static Font AutoScaleFont(Font value, SizeF scaleFactor)
Parameter | Type | Description |
---|---|---|
value | Font | The value to be scaled. |
scaleFactor | SizeF | The scale factor defined by Width. |
Returns
Remarks
This method assumes that fonts are automatically scaling to the system DPI and only per-monitor aware applications running in mixed DPI mode will have to adjust the font.
AutoScaleFont(Font, int)
Automatically rescales a font if the system DPI is different than the DPI of the device where the font will be used.
public static Font AutoScaleFont(Font value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Font | The value to be scaled. |
deviceDpi | int | The DPI of the device where the Font will be used (e.g., 96). |
Returns
Remarks
This method assumes that fonts are automatically scaling to the system DPI and only per-monitor aware applications running in mixed DPI mode will have to adjust the font.
AutoScaleFontSize(float, SizeF)
Automatically rescales a font size if the scale factor for system DPI is different than scale factor where the font will be used.
public static float AutoScaleFontSize(float value, SizeF scaleFactor)
Parameter | Type | Description |
---|---|---|
value | float | The value to be scaled. |
scaleFactor | SizeF | The scale factor defined by Width. |
Returns
- float:
The font size scaled, as needed, to match the DPI of the device.
Remarks
This method assumes that fonts are automatically scaling to the system DPI and only per-monitor aware applications running in mixed DPI mode will have to adjust the font.
AutoScaleFontSize(float, int)
Automatically rescales a font size if the system DPI is different than the DPI of the device where the font will be used.
public static float AutoScaleFontSize(float value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | float | The value to be scaled. |
deviceDpi | int | The DPI of the device where the Font will be used (e.g., 96). |
Returns
- float:
The font size scaled, as needed, to match the DPI of the device.
Remarks
This method assumes that fonts are automatically scaling to the system DPI and only per-monitor aware applications running in mixed DPI mode will have to adjust the font.
AutoScalePadding(Padding, SizeF)
Automatically rescales a Padding value if the current run-time environment does not scale Padding; otherwise, the original value is returned unscaled.
public static Padding AutoScalePadding(Padding value, SizeF scaleFactor)
Parameter | Type | Description |
---|---|---|
value | Padding | The value to be scaled. |
scaleFactor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
- Padding:
The Padding scaled, as needed, based on run-time environment support.
Remarks
This method assumes automatic scaling is applied for .NET 6 and greater. Earlier versions of .NET and all versions of .NET Framework will scale the value.
AutoScalePadding(Padding, int)
Automatically rescales a Padding value if the current run-time environment does not scale Padding; otherwise, the original value is returned unscaled.
public static Padding AutoScalePadding(Padding value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Padding | The value to be scaled. |
deviceDpi | int | The DPI of the device (e.g., 96). |
Returns
- Padding:
The Padding scaled, as needed, based on run-time environment support.
Remarks
This method assumes automatic scaling is applied for .NET 6 and greater. Earlier versions of .NET and all versions of .NET Framework will scale the value.
DescaleFont(Font, SizeF)
Descales the size of a Font from the given factor.
public static Font DescaleFont(Font value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Font | The value to descale. |
factor | SizeF | The descale factor defined by Width. |
Returns
DescaleFont(Font, int)
Descales the size of a Font from the given DPI.
public static Font DescaleFont(Font value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Font | The value to descale. |
deviceDpi | int | The DPI of the device. |
Returns
DescaleInt32(int, SizeF)
Descales an int from the given factor.
public static int DescaleInt32(int value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | int | The value to descale. |
factor | SizeF | The descale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
DescaleInt32(int, int)
Descales an int from the given DPI.
public static int DescaleInt32(int value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | int | The value to descale. |
deviceDpi | int | The DPI of the device. |
Returns
DescalePoint(Point, SizeF)
Descales a Point from the given factor.
public static Point DescalePoint(Point value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Point | The value to descale. |
factor | SizeF | The descale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
DescalePoint(Point, int)
Descales a Point from the given DPI.
public static Point DescalePoint(Point value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Point | The value to descale. |
deviceDpi | int | The DPI of the device. |
Returns
DescaleRectangle(Rectangle, SizeF)
Descales a Rectangle from the given factor.
public static Rectangle DescaleRectangle(Rectangle value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Rectangle | The value to descale. |
factor | SizeF | The descale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
DescaleRectangle(Rectangle, int)
Descales a Rectangle from the given DPI.
public static Rectangle DescaleRectangle(Rectangle value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Rectangle | The value to descale. |
deviceDpi | int | The DPI of the device. |
Returns
DescaleSingle(float, SizeF)
Descales a float from the given factor.
public static float DescaleSingle(float value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | float | The value to descale. |
factor | SizeF | The descale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
DescaleSingle(float, int)
Descales a float from the given DPI.
public static float DescaleSingle(float value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | float | The value to descale. |
deviceDpi | int | The DPI of the device. |
Returns
DescaleSize(Size, SizeF)
Descales a Size from the given factor.
public static Size DescaleSize(Size value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Size | The value to descale. |
factor | SizeF | The descale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
DescaleSize(Size, int)
Descales a Size from the given DPI.
public static Size DescaleSize(Size value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Size | The value to descale. |
deviceDpi | int | The DPI of the device. |
Returns
GetDefaultScaledPenWidth(SizeF)
Get the default width for a Pen scaled to the given DPI scale.
public static float GetDefaultScaledPenWidth(SizeF scaleFactor)
Parameter | Type | Description |
---|---|---|
scaleFactor | SizeF | The scale factor (e.g., |
Returns
GetDefaultScaledPenWidth(int)
Get the default width for a Pen scaled to the given DPI.
public static float GetDefaultScaledPenWidth(int deviceDpi)
Parameter | Type | Description |
---|---|---|
deviceDpi | int | The DPI of the device. |
Returns
GetDeviceDpi(Control)
Gets the current DPI of the device where the control is currently displayed.
public static int GetDeviceDpi(Control control)
Parameter | Type | Description |
---|---|---|
control | Control | The control to examine for DPI. |
Returns
- int:
The current device DPI (e.g.,
96
for 100% DPI,192
for 200% DPI).
GetDeviceDpiChangeFactor(int, int)
Gets the factor required to scale from deviceDpiOld
to deviceDpiNew
.
public static SizeF GetDeviceDpiChangeFactor(int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
deviceDpiOld | int | The DPI of the old device (e.g., 96), or |
deviceDpiNew | int | The DPI of the new device (e.g., 96), or |
Returns
GetDpiAwarenessDescription()
Gets a description of the DPI awareness context of the currently executing thread.
public static string GetDpiAwarenessDescription()
Returns
- string:
A description of the DPI awareness context.
See Also
GetDpiScale(Graphics)
Gets the current DPI as a percentage.
public static SizeF GetDpiScale(Graphics g)
Parameter | Type | Description |
---|---|---|
g | Graphics | The |
Returns
GetDpiScale(int)
Gets the current DPI as a percentage.
public static SizeF GetDpiScale(int deviceDpi)
Parameter | Type | Description |
---|---|---|
deviceDpi | int | The DPI of the device (e.g., 96). |
Returns
GetDpiScale(Control)
Gets the current DPI of the control as a percentage.
public static SizeF GetDpiScale(Control control)
Parameter | Type | Description |
---|---|---|
control | Control | The control to examine for DPI scale. |
Returns
GetDpiScale(Screen)
Gets the current DPI of the screen as a percentage.
public static SizeF GetDpiScale(Screen screen)
Parameter | Type | Description |
---|---|---|
screen | Screen | The screen to examine for DPI scale. |
Returns
GetScaledImage(Image, Size)
Gets an image scaled to the given size.
public static Image GetScaledImage(Image sourceImage, Size size)
Parameter | Type | Description |
---|---|---|
sourceImage | Image | The image to scale. |
size | Size | The desired size of the scaled image. |
Returns
- Image:
A new image scaled to the given size.
GetScaledImage(Image, SizeF)
Gets an image scaled by the given factor.
public static Image GetScaledImage(Image sourceImage, SizeF scaleFactor)
Parameter | Type | Description |
---|---|---|
sourceImage | Image | The image to scale. |
scaleFactor | SizeF | The scale factor; e.g., 1.25 for 125% or 2.0 for 200%. |
Returns
- Image:
A new image whose width and height are scaled by the given factor's width and height.
GetScaledImage(Image, int)
Gets an image scaled by the given DPI.
public static Image GetScaledImage(Image sourceImage, int deviceDpi)
Parameter | Type | Description |
---|---|---|
sourceImage | Image | The image to scale. |
deviceDpi | int | The DPI of the device. |
Returns
- Image:
A new image scaled by the given DPI.
GetScaledImageSize(Image, SizeF)
Gets the size of an image after it is scaled.
public static Size GetScaledImageSize(Image image, SizeF scaleFactor)
Parameter | Type | Description |
---|---|---|
image | Image | The image to examine, or |
scaleFactor | SizeF | The scale factor to be applied. |
Returns
- Size:
The size of the image scaled by the given factor, or
Size.Empty
if no image was provided.
GetScaledImageSize(Image, SizeF, bool)
Gets the size of an image after it is scaled.
public static Size GetScaledImageSize(Image image, SizeF scaleFactor, bool allowAutoScale)
Parameter | Type | Description |
---|---|---|
image | Image | The image to examine, or |
scaleFactor | SizeF | The scale factor to be applied. |
allowAutoScale | bool |
|
Returns
- Size:
The size of the image scaled by the given factor, or
Size.Empty
if no image was provided.
GetScaledSystemHorizontalScrollBarArrowWidth(SizeF)
Gets the system-defined width of a horizontal scrollbar arrow for a control with the given DPI scale factor.
public static int GetScaledSystemHorizontalScrollBarArrowWidth(SizeF dpiScaleFactor)
Parameter | Type | Description |
---|---|---|
dpiScaleFactor | SizeF | The scale factor of the control where the scrollbar will be hosted. |
Returns
- int:
The scaled width of a horizontal scrollbar arrow.
GetScaledSystemHorizontalScrollBarHeight(SizeF)
Gets the system-defined height of a horizontal scrollbar for a control with the given DPI scale factor.
public static int GetScaledSystemHorizontalScrollBarHeight(SizeF dpiScaleFactor)
Parameter | Type | Description |
---|---|---|
dpiScaleFactor | SizeF | The scale factor of the control where the scrollbar will be hosted. |
Returns
- int:
The scaled height of a horizontal scrollbar.
GetScaledSystemVerticalScrollBarArrowHeight(SizeF)
Gets the system-defined height of a vertical scrollbar arrow for a control with the given DPI scale factor.
public static int GetScaledSystemVerticalScrollBarArrowHeight(SizeF dpiScaleFactor)
Parameter | Type | Description |
---|---|---|
dpiScaleFactor | SizeF | The scale factor of the control where the scrollbar will be hosted. |
Returns
- int:
The scaled height of a vertical scrollbar arrow.
GetScaledSystemVerticalScrollBarWidth(SizeF)
Gets the system-defined width of a vertical scrollbar for a control with the given DPI scale factor.
public static int GetScaledSystemVerticalScrollBarWidth(SizeF dpiScaleFactor)
Parameter | Type | Description |
---|---|---|
dpiScaleFactor | SizeF | The scale factor of the control where the scrollbar will be hosted. |
Returns
- int:
The scaled width of a vertical scrollbar.
GetScreenDeviceDpi(Point)
Gets the current DPI of the screen which contains the given location.
public static int GetScreenDeviceDpi(Point screenLocation)
Parameter | Type | Description |
---|---|---|
screenLocation | Point | The screen location. |
Returns
- int:
The current device DPI (e.g.,
96
for 100% DPI,192
for 200% DPI).
Remarks
In per-monitor aware applications, this method should only be used when a window/control cannot be used.
GetScreenDeviceDpi(Screen)
Gets the current DPI of the given screen.
public static int GetScreenDeviceDpi(Screen screen)
Parameter | Type | Description |
---|---|---|
screen | Screen | The screen to examine. |
Returns
- int:
The current device DPI (e.g.,
96
for 100% DPI,192
for 200% DPI).
Remarks
In per-monitor aware applications, this method should only be used when a window/control cannot be used.
GetSystemDeviceDpi()
Gets the current DPI of the default system device.
public static int GetSystemDeviceDpi()
Returns
- int:
The current device DPI (e.g.,
96
for 100% DPI,192
for 200% DPI).
GetSystemDpiScale()
Gets the current DPI of the default system device as a percentage.
public static SizeF GetSystemDpiScale()
Returns
RescaleFont(Font, SizeF, SizeF)
Rescales a font by changing the size relative to the scale factor.
public static Font RescaleFont(Font value, SizeF scaleFactorOld, SizeF scaleFactorNew)
Parameter | Type | Description |
---|---|---|
value | Font | The value to be rescaled. |
scaleFactorOld | SizeF | The old scale factor (e.g., |
scaleFactorNew | SizeF | The new scale factor (e.g., |
Returns
RescaleFont(Font, int, int)
Rescales a font by changing the size relative to the device DPI.
public static Font RescaleFont(Font value, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
value | Font | The value to be rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
Returns
RescaleInt32(int, int, int)
Rescales a int by changing the value relative to the device DPI.
public static int RescaleInt32(int value, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
value | int | The value to rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
Returns
RescaleListViewColumns(ListView, int, int)
public static void RescaleListViewColumns(ListView listView, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
listView | ListView | The ListView whose columns will be rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
RescalePoint(Point, int, int)
Rescales a Point by changing the value relative to the device DPI.
public static Point RescalePoint(Point value, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
value | Point | The value to rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
Returns
RescaleRectangle(Rectangle, int, int)
Rescales a Rectangle by changing the value relative to the device DPI.
public static Rectangle RescaleRectangle(Rectangle value, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
value | Rectangle | The value to rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
Returns
RescaleSingle(float, int, int)
Rescales a float by changing the value relative to the device DPI.
public static float RescaleSingle(float value, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
value | float | The value to rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
Returns
RescaleSize(Size, int, int)
Rescales a Size by changing the value relative to the device DPI.
public static Size RescaleSize(Size value, int deviceDpiOld, int deviceDpiNew)
Parameter | Type | Description |
---|---|---|
value | Size | The value to rescaled. |
deviceDpiOld | int | The DPI of the old device (e.g., 96). |
deviceDpiNew | int | The DPI of the new device (e.g., 96). |
Returns
ScaleFactorToDeviceDpi(SizeF)
Converts a DPI scale factor into the equivalent device DPI.
public static int ScaleFactorToDeviceDpi(SizeF factor)
Parameter | Type | Description |
---|---|---|
factor | SizeF | The scale factor defined by Width. |
Returns
- int:
The equivalent device DPI for the scale factor.
ScaleInt32(int, SizeF)
Scales an int by a given factor.
public static int ScaleInt32(int value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | int | The value to scale. |
factor | SizeF | The scale factor defined by Width. |
Returns
ScaleInt32(int, int)
Scales an int for the given DPI.
public static int ScaleInt32(int value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | int | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScalePadding(Padding, SizeF)
Scales a Padding by a given factor.
public static Padding ScalePadding(Padding value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Padding | The value to scale. |
factor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
ScalePadding(Padding, int)
Scales a Padding by the given DPI.
public static Padding ScalePadding(Padding value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Padding | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScalePadding(Padding, SizeF)
Scales a Padding by a given factor.
public static Padding ScalePadding(Padding value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Padding | The value to scale. |
factor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
ScalePadding(Padding, int)
Scales a Padding by the given DPI.
public static Padding ScalePadding(Padding value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Padding | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScalePenWidth(float, SizeF)
Scales the width for a Pen to the given scale.
public static float ScalePenWidth(float value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | float | The value to scale. |
factor | SizeF | The scale factor (e.g., |
Returns
ScalePenWidth(float, int)
Scales the width for a Pen to the given scale.
public static float ScalePenWidth(float value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | float | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScalePoint(Point, SizeF)
Scales a Point by a given factor.
public static Point ScalePoint(Point value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Point | The value to scale. |
factor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
ScalePoint(Point, int)
Scales a Point to the given DPI.
public static Point ScalePoint(Point value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Point | The value to scale. |
deviceDpi | int | The device DPI. |
Returns
ScaleRectangle(Rectangle, SizeF)
Scales a Rectangle by a given factor.
public static Rectangle ScaleRectangle(Rectangle value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Rectangle | The value to scale. |
factor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
ScaleRectangle(Rectangle, int)
Scales a Rectangle to the given DPI.
public static Rectangle ScaleRectangle(Rectangle value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Rectangle | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScaleSingle(float, SizeF)
Scales a float by a given factor.
public static float ScaleSingle(float value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | float | The value to scale. |
factor | SizeF | The scale factor defined by Width. |
Returns
ScaleSingle(float, int)
Scales a float to the given DPI.
public static float ScaleSingle(float value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | float | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScaleSize(Size, SizeF)
Scales a Size by a given factor.
public static Size ScaleSize(Size value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | Size | The value to scale. |
factor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
ScaleSize(Size, int)
Scales a Size to the given DPI.
public static Size ScaleSize(Size value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | Size | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
ScaleSizeF(SizeF, SizeF)
Scales a SizeF by a given factor.
public static SizeF ScaleSizeF(SizeF value, SizeF factor)
Parameter | Type | Description |
---|---|---|
value | SizeF | The value to scale. |
factor | SizeF | The scale factor where Width is applied to the x-axis, and Height is applied to the y-axis. |
Returns
ScaleSizeF(SizeF, int)
Scales a SizeF to the given DPI.
public static SizeF ScaleSizeF(SizeF value, int deviceDpi)
Parameter | Type | Description |
---|---|---|
value | SizeF | The value to scale. |
deviceDpi | int | The DPI of the device. |
Returns
Fields
DefaultDeviceDpi
The default device DPI.
public const int DefaultDeviceDpi = 96