Как растянуть изображение в кнопке
Сделал вот такую не замысловатую конструкцию: Но не получается сделать, чтобы фотка, которая будет помещена в ImageButtom могла выбирать размеры такие как вот имеет: BackgroundImage Помогите сделать...
private Image _image;
protected override void OnPaint(PaintEventArgs e)
{
if (_image != null)
{
e.Graphics.DrawImage(_image, (Width / 2) - (_image.Width / 2), (Height / 2) - (_image.Height / 2));
}
public Image ImageButtom
{
get
{
return _image;
}
set
{
_image = value;
RecreateHandle();
}
}
}
Ответы (1 шт):
Автор решения: t3f
→ Ссылка
// Stretch
e.Graphics.DrawImage(_image, this.ClientRectangle);
// Center
int left = (this.ClientSize.Width - _image.Width) / 2;
int top = (this.ClientSize.Height - _image.Height) / 2;
e.Graphics.DrawImage(_image, left, top);
// Tile
using (var texture = new TextureBrush(_image)) {
e.Graphics.FillRectangle(texture, this.ClientRectangle);
}
// Zoom
double xr = (double)this.ClientSize.Width / _image.Width ;
double yr = (double)this.ClientSize.Height / _image.Height;
Rectangle rect = this.ClientRectangle;
if (xr > yr) {
rect.Width = (int)(_image.Width * yr);
rect.X = (this.ClientSize.Width - rect.Width) / 2;
} else {
rect.Height = (int)(_image.Height * xr);
rect.Y = (this.ClientSize.Height - rect.Height) / 2;
}
e.Graphics.DrawImage(_image, rect);