Compartir a través de


DpiHelper Class

Definition

Caution

Use Microsoft.VisualStudio.Utilities.DpiAwareness instead.

Helper class for scaling coordinates and images according to current DPI scaling set in Windows for the screen.

This class can be used in assemblies that can't take a dependency on Microsoft.VisualStudio.Shell.14.0 (or later) This class doesn't use any VS-specific interfaces or services.

The code running in Visual Studio or other appids should not be using this class. Instead, it should use the static DpiHelper class from Microsoft.VisualStudio.PlatformUI namespace from the VS Framework assembliy.

Usually the code needing Dpi scaling functionality provided by this class will want to access it by calling the members of the static DpiHelper.Default instance.

public ref class DpiHelper
[Windows::Foundation::Metadata::WebHostHidden]
public ref class DpiHelper
[Windows::Foundation::Metadata::WebHostHidden]
class DpiHelper
public class DpiHelper
[System.Obsolete("Use Microsoft.VisualStudio.Utilities.DpiAwareness instead.")]
public class DpiHelper
type DpiHelper = class
[<System.Obsolete("Use Microsoft.VisualStudio.Utilities.DpiAwareness instead.")>]
type DpiHelper = class
Public Class DpiHelper
Inheritance
DpiHelper
Derived
Attributes

Remarks

This class can be used in assemblies that can't take a dependency on Microsoft.VisualStudio.Shell.14.0 (or later). This class doesn't use any Visual Studio-specific interfaces or services. The code running in Visual Studio or other appids should not be using this class. Instead, it should use the static DpiHelper class from Microsoft.VisualStudio.PlatformUI namespace from the Visual Studio Framework assembly. Usually the code needing Dpi scaling functionality provided by this class should access it by calling the members of the static DpiHelper.Default instance.

Constructors

DpiHelper(Double)

Protected constructor. To use the functionality of this clas, use DpiHelper.Default or call DpiHelper.GetHelper() public methods.

Fields

DefaultLogicalDpi

The default logical DPI.

Properties

BitmapScalingMode

Retuns the BitmapScalingMode algorithm to be used for resizing images in WPF. This allows the shell to control the algorithm depending on the DPI zoom scale, and allows the user to override it via registry settings like General\BitmapScalingXXX = (BitmapScalingMode)value, with XXX the zoom factor in percents, e.g. BitmapScaling150, etc. Note that for WPF scaling, there is an additional registry value that applies for zoom factors > 200%, UseBitmapPreScalingXXX

Default

Returns a DpiHelper class suitable for scaling images designed for 100% DPI zoom levels (96dpi). The static member can be used in bindings like DpiHelper.Default.BitmapScalingMode or direct properties/methods calls like DpiHelper.Default.LogicalToDevicePixels(), similar to the way the static class DpiHelper in Microsoft.VisualStudio.PlatformUI namespace can be used in VS-specific assemblies to bind to DpiHelper.BitmapScalingMode or call DpiHelper.LogicalToDevicePixels()

DeviceDpiX

The screen's (device) current horizontal DPI

DeviceDpiY

The screen's (device) current vertical DPI

DeviceToLogicalUnitsScalingFactorX

Converts to the horizontal logical units scaling factor.

DeviceToLogicalUnitsScalingFactorY

Converts to the vertical logical units scaling factor.

DpiScalePercentX
DpiScalePercentY
ImageScalingMode

Retuns the ImageScalingMode algorithm to be used for resizing images in WinForms/Win32. This allows the shell to control the algorithm depending on the DPI zoom scale, and allows the user to override it via registry settings like General\ImageScalingXXX = (ImageScalingMode)value, with XXX the zoom factor in percents, e.g. ImageScaling150, etc.

IsScalingRequired

Returns whether scaling is required when converting between logical-device units

LogicalDpiX

The helper's logical horizontal DPI

LogicalDpiY

The helper's logical vertical DPI

LogicalToDeviceUnitsScalingFactorX
LogicalToDeviceUnitsScalingFactorY
PreScaledImageLayoutTransformScaleX

Return the horizontal scale value that should be used with a LayoutTransform/ScaleTransform to scale back an image pre-scaled in HighDPI with DpiPrescaleImageSourceConverter in order to obtain crisp results

PreScaledImageLayoutTransformScaleY

Return the vertical scale value that should be used with a LayoutTransform/ScaleTransform to scale back an image pre-scaled in HighDPI with DpiPrescaleImageSourceConverter in order to obtain crisp results

TransformFromDevice
TransformToDevice
UsePreScaledImages

Returns whether images should be pre-scaled at zoom levels higher than 200%, using NearestNeighbor up to the largest multiple of 100%

Methods

CreateDeviceFromLogicalImage(Bitmap, Size, Color, ImageScalingMode)

Creates and returns a new bitmap strip from the logical System.Drawing.Bitmap scaled for the device units When displayed on the device, the scaled image strip will have same size as the original image strip would have when displayed at 96dpi.

This should be used for converting imagelists bitmap strips instead of simply scaling the bitmap, because the resultant bitmap strip size may be different from just upscaling as bitmap N * Math.Round(width * dpiscale) != Math.Round(N * width * dpiscale) This may happen depending on the image width because dpiscale is a double not an int.

CreateDeviceFromLogicalImage(Bitmap, Size, ImageScalingMode)

Creates and returns a new bitmap strip from the logical System.Drawing.Bitmap strip scaled for the device units, using NearestNeighbor interpolation. When displayed on the device, the scaled image strip will have same size as the original image strip would have when displayed at 96dpi.

This should be used for converting imagelists bitmap strips instead of simply scaling the bitmap, because the resultant bitmap strip size may be different from just upscaling as bitmap N * Math.Round(width * dpiscale) != Math.Round(N * width * dpiscale) This may happen depending on the image width because dpiscale is a double not an int.

CreateDeviceFromLogicalImage(Icon, ImageScalingMode)

Creates and returns a new icon scaled for the device units. When displayed on the device, the scaled icon will have same size as the original icon would have when displayed at 96dpi.

CreateDeviceFromLogicalImage(Image, Color, ImageScalingMode)

Creates and returns a new bitmap or metafile from the logical System.Drawing.Image scaled for the device units. When displayed on the device, the scaled image will have same size as the original image would have when displayed at 96dpi.

CreateDeviceFromLogicalImage(Image, ImageScalingMode)

Creates and returns a new bitmap or metafile from the logical System.Drawing.Image scaled for the device units. When displayed on the device, the scaled image will have same size as the original image would have when displayed at 96dpi.

CreateDeviceFromLogicalImage(ImageList, Color, ImageScalingMode)

Creates and returns a new imagelist scaled for the device units When displayed on the device, the scaled imagelist will have same size as the original imagelist would have when displayed at 96dpi.

CreateDeviceFromLogicalImage(ImageList, ImageScalingMode)

Creates and returns a new imagelist scaled for the device units When displayed on the device, the scaled imagelist will have same size as the original imagelist would have when displayed at 96dpi.

DeviceToLogicalUnits(Point)

Returns a new Point with the input's coordinates converted from device units to logical units.

DeviceToLogicalUnits(Rect)

Returns a new Rect with the input's coordinates converted from device units to logical units.

DeviceToLogicalUnits(Size)

Returns a new Size with the input's dimensions converted from device units to logical units.

DeviceToLogicalUnits(Thickness)

Returns a new Thickness with the input's dimensions converted from device units to logical units.

DeviceToLogicalUnitsX(Double)

Transforms a horizontal coordinate from device to logical units

DeviceToLogicalUnitsX(Int32)

Transforms a horizontal integer coordinate from device to logical units

DeviceToLogicalUnitsX(Single)

Transforms a horizontal coordinate from device to logical units

DeviceToLogicalUnitsY(Double)

Transforms a vertical coordinate from device to logical units

DeviceToLogicalUnitsY(Int32)

Transforms a vertical integer coordinate from device to logical units

DeviceToLogicalUnitsY(Single)

Transforms a vertical coordinate from device to logical units

GetBitmapScalingModeOverride(Int32, BitmapScalingMode)

Gets the bitmap scaling mode override.

GetDeviceActualSize(FrameworkElement)

Get the FrameworkElement's Size in device units

GetDeviceHeight(Window)

Gets the window's Height in device units

GetDeviceLeft(Window)

Gets the window's Left coordinate in device units

GetDeviceRect(Window)

Gets the window's Rectangle in device units

GetDeviceTop(Window)

Gets the window's Top coordinate in device units

GetDeviceWidth(Window)

Gets the window's Width in device units

GetHelper(Int32)

Returns a DpiHelper that can scale images created for the specified DPI zoom factor. E.g. If you have images that were designed for multiple resolutions (e.g. one image for 96dpi, other for 192dpi, etc), and need to use the 192dpi image to scale it to the actual display DPI, you'll need to call GetHelper(200) to get a DpiHelper that will scale correctly that image.

GetImageScalingModeOverride(Int32, ImageScalingMode)
GetUsePreScaledImagesOverride(Int32, Boolean)
LogicalToDeviceUnits(Bitmap, Color, ImageScalingMode)

Converts (if necessary) the image by scaling it to device units. When displayed on the device, the scaled image will have same size as the original image would have when displayed at 96dpi.

LogicalToDeviceUnits(Bitmap, ImageScalingMode)

Converts (if necessary) the image by scaling it to device units. When displayed on the device, the scaled image will have same size as the original image would have when displayed at 96dpi.

LogicalToDeviceUnits(Bitmap, Size, Color, ImageScalingMode)

Converts (if necessary) the image strip to device units, using NearestNeighbor interpolation.. When displayed on the device, the scaled image strip will have same size as the original image strip would have when displayed at 96dpi.

This should be used for converting imagelists bitmap strips instead of simply scaling the bitmap, because the resultant bitmap strip size may be different from just upscaling as bitmap N * Math.Round(width * dpiscale) != Math.Round(N * width * dpiscale) This may happen depending on the image width because dpiscale is a double not an int.

LogicalToDeviceUnits(Bitmap, Size, ImageScalingMode)

Converts (if necessary) the image strip to device units, using NearestNeighbor interpolation.. When displayed on the device, the scaled image strip will have same size as the original image strip would have when displayed at 96dpi.

This should be used for converting imagelists bitmap strips instead of simply scaling the bitmap, because the resultant bitmap strip size may be different from just upscaling as bitmap N * Math.Round(width * dpiscale) != Math.Round(N * width * dpiscale) This may happen depending on the image width because dpiscale is a double not an int.

LogicalToDeviceUnits(Icon, ImageScalingMode)

Converts (if necessary) the icon by scaling it to device units. When displayed on the device, the scaled icon will have same size as the original icon would have when displayed at 96dpi.

LogicalToDeviceUnits(Image, Color, ImageScalingMode)

Converts (if necessary) the image by scaling it to device units. When displayed on the device, the scaled image will have same size as the original image would have when displayed at 96dpi.

LogicalToDeviceUnits(Image, ImageScalingMode)

Converts (if necessary) the image by scaling it to device units. When displayed on the device, the scaled image will have same size as the original image would have when displayed at 96dpi.

LogicalToDeviceUnits(ImageList, Color, ImageScalingMode)

Converts (if necessary) the imagelist by scaling it to device units. When displayed on the device, the scaled imagelist will have same size as the original imagelist would have when displayed at 96dpi.

LogicalToDeviceUnits(ImageList, ImageScalingMode)

Converts (if necessary) the imagelist by scaling it to device units. When displayed on the device, the scaled imagelist will have same size as the original imagelist would have when displayed at 96dpi.

LogicalToDeviceUnits(Point)

Returns a new Point with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(Point)

Returns a new System.Drawing.Point with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(PointF)

Returns a new System.Drawing.PointF with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(Rect)

Returns a new Rect with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(Rectangle)

Returns a new System.Drawing.Rectangle with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(RectangleF)

Returns a new System.Drawing.RectangleF with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(Size)

Returns a new System.Drawing.Size with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(Size)

Returns a new Size with the input's dimensions converted from logical units to device units.

LogicalToDeviceUnits(SizeF)

Returns a new System.Drawing.SizeF with the input's coordinates converted from logical units to device units.

LogicalToDeviceUnits(Thickness)

Returns a new Thickness with the input's dimensions converted from logical units to device units.

LogicalToDeviceUnitsX(Double)

Transforms a horizontal coordinate from logical to device units

LogicalToDeviceUnitsX(Int32)

Transforms a horizontal integer coordinate from logical to device units by scaling it up for current DPI and rounding to nearest integer value

LogicalToDeviceUnitsX(Single)

Transforms a horizontal coordinate from logical to device units

LogicalToDeviceUnitsY(Double)

Transforms a vertical coordinate from logical to device units

LogicalToDeviceUnitsY(Int32)

Transforms a vertical integer coordinate from logical to device units by scaling it up for current DPI and rounding to nearest integer value

LogicalToDeviceUnitsY(Single)

Transforms a vertical coordinate from logical to device units

RoundToDeviceUnitsX(Double)

Returns the closest value in logical units that will be converted to an integer value if it is converted back to device units using functions for horizontal coordinates/widths such as DeviceToLogicalUnitsX.

RoundToDeviceUnitsY(Double)

Returns the closest value in logical units that will be converted to an integer value if it is converted back to device units using functions for vertical coordinates/widths such as DeviceToLogicalUnitsY.

ScaleLogicalImageForDeviceSize(ImageSource, Size, BitmapScalingMode)

Scale a WPF image to the indicated size, using the specified scalingMode

SetDeviceHeight(Window, Double)

Sets the window's Height using device units

SetDeviceLeft(Window, Double)

Sets the window's Left coordinate using device units

SetDeviceTop(Window, Double)

Sets the window's Top coordinate using device units

SetDeviceWidth(Window, Double)

Sets the window's Width using device units

Applies to