簡體   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