繁体   English   中英

如何在C#中调整按钮背景图像的大小?

[英]How to resize button background image in C#?

如何在C#中调整按钮backgroundImage大小? 我只能找到获取按钮backgroundImage大小的属性。 没有设置大小。

我使用WinForms

BackgroundImageLayout会有所帮助...如果不是,则1)接一个面板(panel1)。 2)将Button Event绑定到panel(panel1)3)在上面的Panel1中添加另一个面板(panel2),将要设置的背景图像和BackgroundImageLayout属性设置为ImageLayout。拉伸4)然后调整panel2的大小,这将重新调整图像的大小这有帮助

我不会比WinForms中的任何简单:

private void yourbutton_Paint(object sender, PaintEventArgs e)
{
    // base.OnPaint(e);     optional
    Rectangle rc = yourButton.ClientRectangle;
    Rectangle ri = new Rectangle(Point.Empty, yourButton.BackgroundImage.Size);
    // e.Graphics.FillRectangle(SystemBrushes.Control, rc);  optional
    e.Graphics.DrawImage(yourButton.BackgroundImage, rc, ri, GraphicsUnit.Pixel);
    e.Graphics.DrawString(yourButton.Text, yourButton.Font, 
                          SystemBrushes.ControlText, Point.Empty);   // if needed
}

如果看一下代码,您会发现它实际上实际上只是一行,如果按钮上有文本,则实际上是两行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM