So despite the MS philosophy is to go toward out-of-the-box stretched images for Windows Form Controls when high DPI, it seems images on Button need to be stretched manually. Of course an even better solution would be that, for each bitmap shown to user (on button and everywhere else) to define several bitmaps adapted to 250% 200% 150% and 125% DPI.
Here is the code:
public static IEnumerable<IDisposable> AdjustControlsThroughDPI(this Control.ControlCollection controls) {
Debug.Assert(controls != null);
if (DPIRatioIsOne) {
return new IDisposable[0]; // No need to adjust on DPI One
}
var list = new List<IDisposable>();
foreach (Control control in controls) {
if (control == null) { continue; }
var button = control as ButtonBase;
if (button != null) {
button.AdjustControlsThroughDPI(list);
continue;
}
// Here more controls tahn button can be adjusted if needed...
// Recursive
var nestedControls = control.Controls;
Debug.Assert(nestedControls != null);
if (nestedControls.Count == 0) { continue; }
var disposables = nestedControls.AdjustControlsThroughDPI();
list.AddRange(disposables);
}
return list;
}
private static void AdjustControlsThroughDPI(this ButtonBase button, IList<IDisposable> list) {
Debug.Assert(button != null);
Debug.Assert(list != null);
var image = button.Image;
if (image == null) { return; }
var imageStretched = image.GetImageStretchedDPI();
button.Image = imageStretched;
list.Add(imageStretched);
}
private static Image GetImageStretchedDPI(this Image imageIn) {
Debug.Assert(imageIn != null);
var newWidth = imageIn.Width.MultipliedByDPIRatio();
var newHeight = imageIn.Height.MultipliedByDPIRatio();
var newBitmap = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(newBitmap)) {
// According to this blog post http://blogs.msdn.com/b/visualstudio/archive/2014/03/19/improving-high-dpi-support-for-visual-studio-2013.aspx
// NearestNeighbor is more adapted for 200% and 200%+ DPI
var interpolationMode = InterpolationMode.HighQualityBicubic;
if (s_DPIRatio >= 2.0f) {
interpolationMode = InterpolationMode.NearestNeighbor;
}
g.InterpolationMode = interpolationMode;
g.DrawImage(imageIn, new Rectangle(0, 0, newWidth, newHeight));
}
imageIn.Dispose();
return newBitmap;
}
Notice that an enumerable of disposable bitmaps created is returned. If you don't care disposing bitmap on buttons, you won't have to care for disposing stretched bitmap.
Notice we dispose original buttons bitmaps.
Notice our own members to deal with DPI: MultipliedByDPIRatio(this int)
, DPIRatioIsOne:bool
, s_DPIRatio
. You can write your own, the tricky point is to obtain the actual DPI ratio. To gather DPI ratio the best way I found is this one.
Notice the reference to the blog post Improving High-DPI support for Visual Studio 2013 where the VS team explains that for their icon style, they determine that image stretched between ] 200%, 100% [ is best achieved with Bicubic algorithm, and above or equal to 200%, is best achieved with naive nearest neighbor algorithm. The code presented reflects these choices.
Edit: below screenshot of various interpolation mode at 200% DPI, IMHO InterpolationMode.HighQualityBicubic
is better than InterpolationMode.NearestNeighbor
.