Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

C# WinForms: square background image not shown as square with BackgroundImageLayout.Zoom

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:
enter image description here
The size of the "plus" on the left button is 41 x 49. On the right button, it's 49 x 41.

  1. Why?
  2. 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:
enter image description here
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...