我想获取图片以填充图片框,但不留下任何空格。因此,当图像的长宽比不调整时,剪切图像的部分以适合它。并在用户调整窗口/图片框大小时进行调整。现有选项Sizemode = Zoom
会留下空白,因为它不敢切断任何图像,并且会延伸图像,使图像变形。调整图片的大小以填充图片框而不拉伸
我能想到的唯一方法是创建一个算法来调整图像的大小,保持对比度,并将图像的宽度或长度设置为pictureBox的宽度或长度,并创建一些运行时环路,算法一次一帧。它看起来有点性能沉重,它的作用和种类很诡异。有更好的选择吗?
编辑: 对于没有人来用,我实现了伊万Stoev的解决方案略有不同:
class ImageHandling
{
public static Rectangle GetScaledRectangle(Image img, Rectangle thumbRect)
{
Size sourceSize = img.Size;
Size targetSize = thumbRect.Size;
float scale = Math.Max((float) targetSize.Width/sourceSize.Width, (float) targetSize.Height/sourceSize.Height);
var rect = new RectangleF();
rect.Width = scale * sourceSize.Width;
rect.Height = scale * sourceSize.Height;
rect.X = (targetSize.Width - rect.Width)/2;
rect.Y = (targetSize.Height - rect.Height)/2;
return Rectangle.Round(rect);
}
public static Image GetResizedImage(Image img, Rectangle rect)
{
Bitmap b = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage((Image) b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, 0, 0, rect.Width, rect.Height);
g.Dispose();
try
{
return (Image)b.Clone();
}
finally
{
b.Dispose();
b = null;
g = null;
}
}
public Form1()
{
InitializeComponent();
updateMainBackground();
}
void updateMainBackground()
{
Image img = Properties.Resources.BackgroundMain;
Rectangle newRect = ImageHandling.GetScaledRectangle(img, mainBackground.ClientRectangle);
mainBackground.Image = ImageHandling.GetResizedImage(img, newRect);
}
private void Form1_Resize(object sender, EventArgs e)
{
updateMainBackground();
}
}
完美的作品,非常感谢。 – daboss42
你有没有java的方法?谢谢 –