【问题标题】:Making a background image scale with button size使用按钮大小制作背景图像
【发布时间】:2012-11-13 07:37:18
【问题描述】:

我正在尝试向我的 Win Forms 应用程序中的几个按钮添加一些背景图像。这三张图片的尺寸不同(即像素尺寸不匹配,一张是 128x128,另一张是 256x256)。我需要按钮大小相同(否则 GUI 非常不对称)。在不更改实际图像文件的情况下,如何使图像随按钮大小缩放?

我尝试创建自己的类,并为按钮调整大小事件添加事件处理程序,但这似乎不起作用。我的代码:

class CustomButton : Button {

        internal void CustomButton_Resize( object sender, EventArgs e ) {
            if ( this.BackgroundImage == null ) {
                return;
            }

            var pic = new Bitmap( this.BackgroundImage, this.Width, this.Height );
            this.BackgroundImage = pic;
        }
    }

形式:

this.buttonOne.Resize += new System.EventHandler(this.buttonOne.CustomButton_Resize);

忘了提一下,上面的代码根本没有调整图像的大小。按钮仍然需要有不同的大小才能完全显示图像。

【问题讨论】:

  • 我曾尝试以简洁的方式编写相同的内容,但未能成功。至少请改善代码的外观:)

标签: c# winforms user-interface


【解决方案1】:

将背景图像添加到 .NET Button 对象并缩放以适合的最简单方法

我使用这种方法来避免对新类和事件处理程序进行任何额外的编码。这也帮助我避免将所有 Button 对象转换为 Image 对象。

  1. 将图像添加到您的 Resources.resx 文件中。

  2. 单击您选择的按钮。

  3. 导航到BackgroundImage 属性并选择您导入到项目的resources.resx 文件中的图像。

  4. 导航到BackgroundImageLayout 属性并选择Stretch

确保您没有为 ImageText 属性输入任何内容,否则它们会干扰您的新背景图片。

【讨论】:

  • 这可能有一个缺点,即禁用按钮时图像不会变灰。在这种情况下,ImageList 可能会有所帮助。
【解决方案2】:

简单的编程方式

假设我有一个按钮 btn1,以下代码在 visual-studio-2010 中完美运行。

private void btn1_Click(object sender, EventArgs e)
{
    btn1.Width = 120;
    btn1.Height = 100;
}
void btn1_Resize(object sender, EventArgs e)
{
    if ( this.BackgroundImage == null )
          return;
    var bm = new Bitmap(btn1.BackgroundImage, new Size(btn1.Width, btn1.Height));
    btn1.BackgroundImage = bm;
}

更好的方法

您可以在自定义按钮的构造函数中添加事件处理程序(只是为了确保您正确添加事件处理程序)

class CustomButton : Button
{    
    CustomButton()
    {
        this.Resize += new System.EventHandler(buttonOne.CustomButton_Resize);
    }
    void CustomButton_Resize( object sender, EventArgs e )
    {
       if ( this.BackgroundImage == null )
          return;
       var pic = new Bitmap( this.BackgroundImage, new Size(this.Width, this.Height) );
       this.BackgroundImage = pic;          
    }
}

现在,当您在任何地方调整按钮的大小时,您的图像将适合(缩放)到其新大小。

【讨论】:

  • 你会认为这样的东西应该已经是 .NET Button 类的一部分了!
【解决方案3】:

你可以从这样的事情开始......

 public class ImageButton : Control
{
    public Image backgroundImage;

    public Image BackgroundImage
    {
        get
        {
            return backgroundImage;
        }
        set
        {
            backgroundImage = value;
            Refresh();
        }
    }

    public ImageButton()
    {

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(BackColor);

        if(BackgroundImage != null)
            e.Graphics.DrawImage(BackgroundImage, 0, 0, Width, Height);

        base.OnPaint(e);
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        //base.OnPaintBackground(pevent);
    }
}

您可以自己处理绘画并绘制图像。您也可以尝试使用 PictureBox 或其他具有更多缩放选项的控件

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2011-09-14
    相关资源
    最近更新 更多