的ImageButton,将自动调整图像 [英] ImageButton that will AutoSize the image

查看:126
本文介绍了的ImageButton,将自动调整图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个按钮控制将自动调整大小的形象。普通按钮控件是不会这样做的。我使用C#.NET 2.0。

I'm searching for a button control that will AutoSize its image. Normal button controls won't do this. I'm using C#.Net 2.0.

例如,我有一个按钮,是200×50像素和图像是800 * 100像素。我要调整大小的图像,它是一个小到左边,按钮的文本附近。随着图片框我可以做到这一点。但是,当我打下了图片框按钮的非常难看,因为你不能点击那里。

For example, I have a Button that is 200 x 50px and an image that is 800 x 100px. I want to resize the Image so that it is a little to the left, near the text of the button. With a PictureBox I can do this. But when I lay a PictureBox over the Button its very ugly because you can't click there.

推荐答案

您可以做到这一点,如下所示:

You can do this as follows:

button.Image = Image.FromFile(path);
button.AutoSize = true;

例如:或者,您可以创建一个将改变图像大小的新按钮类型:

E.g: Or, You can create a new Button type that will change the size of the image:

public class AutoSizeButton : Button
{

    public new Image Image
    {
        get { return base.Image; }
        set 
        {
            Image newImage = new Bitmap(Width, Height);
            using (Graphics g = Graphics.FromImage(newImage))
            {
                g.DrawImage(value, 0, 0, Width, Height);
            }
            base.Image = newImage;
        }
    }
}

测试:

AutoSizeButton button = new AutoSizeButton();
button.Location = new Point(27, 52);
button.Name = "button";
button.Size = new Size(75, 23);
button.Text = "Test";
button.UseVisualStyleBackColor = true;
button.Image = Image.FromFile(path);
Controls.Add(button);

这篇关于的ImageButton,将自动调整图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆