C# 用于自动调整图像大小的ImageButton

C# 用于自动调整图像大小的ImageButton,c#,.net,button,C#,.net,Button,我正在搜索一个按钮控件,它将自动调整其图像大小。普通的按钮控件无法做到这一点。我正在使用C#.NET2.0 例如,我有一个200 x 50像素的按钮和一个800 x 100像素的图像。我想调整图像的大小,使其稍微向左,靠近按钮的文本。有了PictureBox我就能做到这一点。但是,当我在按钮上放置图片盒时,它非常难看,因为你不能点击那里。你可以按如下方式操作: button.Image = Image.FromFile(path); button.AutoSize = true; 例如:或者,

我正在搜索一个按钮控件,它将
自动调整其图像大小。普通的按钮控件无法做到这一点。我正在使用C#.NET2.0


例如,我有一个200 x 50像素的按钮和一个800 x 100像素的图像。我想调整图像的大小,使其稍微向左,靠近按钮的文本。有了
PictureBox
我就能做到这一点。但是,当我在
按钮上放置
图片盒时,它非常难看,因为你不能点击那里。

你可以按如下方式操作:

button.Image = Image.FromFile(path);
button.AutoSize = true;
例如:或者,您可以创建一个新的按钮类型来更改图像的大小:

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;
        }
    }
}
测试:


您可以按如下方式执行此操作:

button.Image = Image.FromFile(path);
button.AutoSize = true;
例如:或者,您可以创建一个新的按钮类型来更改图像的大小:

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;
        }
    }
}
测试:


我最初建议使用标准的ImageButton,但后来阅读了您的评论,您试图根据图像调整按钮的大小。为此,请使用链接按钮:

<asp:LinkButton ID="foo" runat="server" OnClick="LinkButton1_Click">   
    <asp:Image ID="imagefoo" runat="server" ImageUrl="~/Foo.jpg" />
</asp:LinkButton>

我最初建议使用标准的ImageButton,但后来阅读了您的评论,您试图根据图像调整按钮的大小。为此,请使用链接按钮:

<asp:LinkButton ID="foo" runat="server" OnClick="LinkButton1_Click">   
    <asp:Image ID="imagefoo" runat="server" ImageUrl="~/Foo.jpg" />
</asp:LinkButton>

我在vb.net中寻找这个版本,所以我从mykhaylo的答案开始,并对它进行了一些改进。此代码按比例调整图像大小,使图像居中于按钮中,并为图像提供内部填充

此按钮实际上不使用按钮的“Image”属性,并显示应交替设置的属性“AutoScaleImage”

下面是C#version-VB.net的底部。享受吧

[System.ComponentModel.DesignerCategory("")]
public class AutoScaleButton : Button
{

  private Image _AutoScaleImage;
  public Image AutoScaleImage {
    get { return _AutoScaleImage; }
    set {
      _AutoScaleImage = value;
      if (value != null)
        this.Invalidate();
    }
  }

  private int _AutoScaleBorder;
  public int AutoScaleBorder {
    get { return _AutoScaleBorder; }
    set {
      _AutoScaleBorder = value;
      this.Invalidate();
    }
  }

  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
    DrawResizeImage(ref e.Graphics);
  }


  private void DrawResizeImage(Graphics g)
  {
    if (_AutoScaleImage == null)
      return;
    int iB = AutoScaleBorder;
    int iOff = 0;
    Rectangle rectLoc = default(Rectangle);
    Rectangle rectSrc = default(Rectangle);

    Size sizeDst = new Size(Math.Max(0, this.Width - 2 * iB), 
          Math.Max(0, this.Height - 2 * iB));
    Size sizeSrc = new Size(_AutoScaleImage.Width, 
          _AutoScaleImage.Height);
    double ratioDst = sizeDst.Height / sizeDst.Width;
    double ratioSrc = sizeSrc.Height / sizeSrc.Width;

    rectSrc = new Rectangle(0, 0, sizeSrc.Width, sizeSrc.Height);

    if (ratioDst < ratioSrc) {
      iOff = (sizeDst.Width - 
        (sizeDst.Height * sizeSrc.Width / sizeSrc.Height)) / 2;
      rectLoc = new Rectangle(iB + iOff, 
            iB, 
            sizeDst.Height * sizeSrc.Width / sizeSrc.Height, 
            sizeDst.Height);
    } else {
      iOff = (sizeDst.Height - (sizeDst.Width * sizeSrc.Height / sizeSrc.Width)) / 2;
      rectLoc = new Rectangle(iB, 
            iB + iOff, 
            sizeDst.Width, 
            sizeDst.Width * sizeSrc.Height / sizeSrc.Width);
    }

    g.DrawImage(_AutoScaleImage, rectLoc, rectSrc, GraphicsUnit.Pixel);

  }

}
[System.ComponentModel.DesignerCategory(“”)]
公共类自动缩放按钮:按钮
{
私有图像(自动缩放图像);;
公共映像自动缩放映像{
获取{return\u AutoScaleImage;}
设置{
_自动缩放图像=值;
if(值!=null)
这个。使无效();
}
}
私有内部边界;
公共自动缩放边框{
获取{return\u AutoScaleBorder;}
设置{
_自动缩放边框=值;
这个。使无效();
}
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
基础漆(e);
DrawResizeImage(参考e.Graphics);
}
私有void DrawResizeImage(图形g)
{
如果(_AutoScaleImage==null)
返回;
int iB=自动缩放边框;
int-iOff=0;
矩形rectLoc=默认值(矩形);
矩形rectSrc=默认值(矩形);
Size sizeDst=新尺寸(数学最大值(0,此宽度为-2*iB),
Max(0,这个高度-2*iB));
大小SizeSC=新大小(_AutoScaleImage.Width,
_自动缩放图像(高度);
双比例ST=尺寸标准高度/尺寸标准宽度;
双倍比率RC=尺寸高度/尺寸宽度;
rectSrc=新矩形(0,0,sizesc.Width,sizesc.Height);
if(ratioDst
或者我的原始VB.NET版本

<System.ComponentModel.DesignerCategory("")> _
Public Class AutoScaleButton
  Inherits Button

  Private _AutoScaleImage As Image
  Public Property AutoScaleImage() As Image
    Get
      Return _AutoScaleImage
    End Get
    Set(value As Image)
      _AutoScaleImage = value
      If value IsNot Nothing Then Me.Invalidate()
    End Set
  End Property

  Private _AutoScaleBorder As Integer
  Public Property AutoScaleBorder() As Integer
    Get
      Return _AutoScaleBorder
    End Get
    Set(ByVal value As Integer)
      _AutoScaleBorder = value
      Me.Invalidate()
    End Set
  End Property

  Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)
    DrawResizeImage(e.Graphics)
  End Sub

  Private Sub DrawResizeImage(ByRef g As Graphics)

    If _AutoScaleImage Is Nothing Then Exit Sub
    Dim iB As Integer = AutoScaleBorder, iOff As Integer = 0
    Dim rectLoc As Rectangle, rectSrc As Rectangle

    Dim sizeDst As Size = New Size(Math.Max(0, Me.Width - 2 * iB), Math.Max(0, Me.Height - 2 * iB))
    Dim sizeSrc As Size = New Size(_AutoScaleImage.Width, _AutoScaleImage.Height)
    Dim ratioDst As Double = sizeDst.Height / sizeDst.Width
    Dim ratioSrc As Double = sizeSrc.Height / sizeSrc.Width

    rectSrc = New Rectangle(0, 0, sizeSrc.Width, sizeSrc.Height)

    If ratioDst < ratioSrc Then
      iOff = (sizeDst.Width - (sizeDst.Height * sizeSrc.Width / sizeSrc.Height)) / 2
      rectLoc = New Rectangle(iB + iOff, iB, _
                              sizeDst.Height * sizeSrc.Width / sizeSrc.Height, _
                              sizeDst.Height)
    Else
      iOff = (sizeDst.Height - (sizeDst.Width * sizeSrc.Height / sizeSrc.Width)) / 2
      rectLoc = New Rectangle(iB, iB + iOff, _
                              sizeDst.Width, _
                              sizeDst.Width * sizeSrc.Height / sizeSrc.Width)
    End If

    g.DrawImage(_AutoScaleImage, rectLoc, rectSrc, GraphicsUnit.Pixel)

  End Sub

End Class
_
公共类自动缩放按钮
继承按钮
Private\u自动缩放图像作为图像
公共属性AutoScaleImage()作为图像
得到
返回自动缩放图像
结束
设置(值为图像)
_自动缩放图像=值
如果值不是空,则Me.Invalidate()
端集
端属性
Private\u自动缩放边框为整数
公共属性AutoScaleBorder()为整数
得到
返回自动缩放边框
结束
设置(ByVal值为整数)
_自动缩放边框=值
使无效
端集
端属性
受保护的覆盖子OnPaint(如PaintEventArgs)
MyBase.OnPaint(e)
DrawResizeImage(如图形)
端接头
私有子DrawResizeImage(ByRef g作为图形)
如果_AutoScaleImage为空,则退出Sub
Dim iB作为整数=自动缩放边框,iOff作为整数=0
Dim rectLoc为矩形,rectSrc为矩形
Dim sizeDst As Size=新尺寸(数学最大值(0,Me.宽度-2*iB),数学最大值(0,Me.高度-2*iB))
Dim SIZERC As Size=新尺寸(\u AutoScaleImage.Width,\u AutoScaleImage.Height)
尺寸比率ST为双精度=尺寸标准高度/尺寸标准宽度
尺寸比RC为双=尺寸高度/尺寸宽度
rectSrc=新矩形(0,0,sizesc.Width,sizesc.Height)
如果ratioDst
我在vb.net中寻找这个版本,所以我从mykhaylo的答案开始,并对它进行了一些改进。此代码按比例调整图像大小,使图像居中于按钮中,并为图像提供内部填充

此按钮实际上不使用按钮的“Image”属性,并显示应交替设置的属性“AutoScaleImage”

下面是C#version-VB.net的底部。享受吧

[System.ComponentModel.DesignerCategory("")]
public class AutoScaleButton : Button
{

  private Image _AutoScaleImage;
  public Image AutoScaleImage {
    get { return _AutoScaleImage; }
    set {
      _AutoScaleImage = value;
      if (value != null)
        this.Invalidate();
    }
  }

  private int _AutoScaleBorder;
  public int AutoScaleBorder {
    get { return _AutoScaleBorder; }
    set {
      _AutoScaleBorder = value;
      this.Invalidate();
    }
  }

  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
    DrawResizeImage(ref e.Graphics);
  }


  private void DrawResizeImage(Graphics g)
  {
    if (_AutoScaleImage == null)
      return;
    int iB = AutoScaleBorder;
    int iOff = 0;
    Rectangle rectLoc = default(Rectangle);
    Rectangle rectSrc = default(Rectangle);

    Size sizeDst = new Size(Math.Max(0, this.Width - 2 * iB), 
          Math.Max(0, this.Height - 2 * iB));
    Size sizeSrc = new Size(_AutoScaleImage.Width, 
          _AutoScaleImage.Height);
    double ratioDst = sizeDst.Height / sizeDst.Width;
    double ratioSrc = sizeSrc.Height / sizeSrc.Width;

    rectSrc = new Rectangle(0, 0, sizeSrc.Width, sizeSrc.Height);

    if (ratioDst < ratioSrc) {
      iOff = (sizeDst.Width - 
        (sizeDst.Height * sizeSrc.Width / sizeSrc.Height)) / 2;
      rectLoc = new Rectangle(iB + iOff, 
            iB, 
            sizeDst.Height * sizeSrc.Width / sizeSrc.Height, 
            sizeDst.Height);
    } else {
      iOff = (sizeDst.Height - (sizeDst.Width * sizeSrc.Height / sizeSrc.Width)) / 2;
      rectLoc = new Rectangle(iB, 
            iB + iOff, 
            sizeDst.Width, 
            sizeDst.Width * sizeSrc.Height / sizeSrc.Width);
    }

    g.DrawImage(_AutoScaleImage, rectLoc, rectSrc, GraphicsUnit.Pixel);

  }

}
[System.ComponentModel.DesignerCategory(“”)]
公共类自动缩放按钮:按钮
{
私有图像(自动缩放图像);;
公共映像自动缩放映像{
G