I have added a background image to 2 buttons.
The buttons are both rectangular: button1.Size = 50 x 100
, button2.Size = 100 x 50
.
The BackgroundImageLayout
on both buttons is Zoom
.
The image is 256x256 pixels large, the image background is transparent, the "plus" symbol is 252x252 pixels (=> a 2 pixels wide transparent frame around the "plus").
Based on the square image, I expect the size of the "plus" on both buttons to be about 44 x 44. Maybe a litte larger or smaller, but with square aspect ratio, because that's the purpose of BackgroundImageLayout = Zoom
.
The following screenshot shows that the aspect ratio is not preserved:
The size of the "plus" on the left button is 41 x 49. On the right button, it's 49 x 41.
- Why?
- How can I get my expected square aspect ratio?
EDIT
I found that the aspect ratio is almost correct if I choose FlatStyle = Popup
: The "plus" size is 49 x 48, maybe even 49 x 49 and 1px might be hidden behind the button frame. But then, there is no frame around the icon and the "plus" touches the button frame:
With FlatStyle = Standard
, there is a nice frame (see first image), but this frame seems to squeeze the background image vertically. The width is obviously not adjusted.
Any workaround for that?
If possible, without overriding OnPaint() again.
And I don't want to increase the frame in the images.
question from:
https://stackoverflow.com/questions/65904715/c-sharp-winforms-square-background-image-not-shown-as-square-with-backgroundima 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…