如何在 WinForms 按钮的文本前添加图像图标?

How to add an image icon before the text of a WinForms button?

我正在尝试在 WinForms 按钮 87x30 大小的文本前添加 .ico 48x48 图像:

  button1.BackgroundImageLayout = ImageLayout.Stretch;  
  button1.BackgroundImageLayout = ImageLayout.None;                 
  button1.BackgroundImageLayout = ImageLayout.Zoom;  
  button1.ImageAlign = ContentAlignment.MiddleLeft;
  button1.TextImageRelation = TextImageRelation.ImageBeforeText;
  button1.TextAlign = ContentAlignment.MiddleRight;

结果是:

我想知道如何将左侧的图像与相关距离的文本对齐,如下所示:

编辑:

    button1.TextImageRelation = TextImageRelation.ImageBeforeText;
    button1.TextAlign = ContentAlignment.MiddleLeft; /// MiddleRight; // MiddleCenter; 
    button1.ImageAlign = ContentAlignment.MiddleRight; /// MiddleLeft;

结果:

背景图属性就像操作系统的桌面背景,是一张墙纸,可以拉伸、适配、重复...

因此在这里您不需要使用 BackgroundImage 作为与其文本关联的按钮图标样式图像。

如果您使用 Image 属性 并将对齐设置为左对齐,文本右对齐,一切正常:

然后您可以根据图像大小 and/or 文本大小和长度调整这些对齐方式以及宽度和高度以达到所需的结果。


此外,正如我最终发现的 所示,要简单地将所有内容居中,您可以使用 TextImageRelation 并将其设置为 TextImageRelation.ImageBeforeText 而无需更改对齐方式,如有必要通过根据显示图像的大小增加高度以获得干净的结果: