I cannot find a way to make scale the image in a Windows Form Button. See below what it looks like with the Windows Form designer shown on DPI 200% (I am aware that the Windows Form designer should be only used on DPI 100% / 96, this screenshot just illustrates properly my point).
While the button size get scaled properly (34x33), the image in the button size doesn't get scaled/stretched/zoomed (it remains 16x16). I did many attempt to solve this:
- The parent control
AutoScaleMode
is set toFont
, setting it toDpi
doesn't make this work. - Setting button
AutoSize
totrue
orfalse
doesn't make it work. - Setting button or parent control
AutoSizeMode
to any value doesn't make it work. - there is no
Button.ImageLayout
that could be set toStretch
orZoom
. - Using the new
App.Config
setting<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
doesn't make it work. - Changing button
FlatStyle
orImageAlign
doesn't make it work.
How did you get solved this in your app?
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:
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 thanInterpolationMode.NearestNeighbor
.