【发布时间】:2014-07-21 19:59:36
【问题描述】:
我希望用户每次都调整 Form 的大小,pictureBox 中的 Image 也用相同的 Values 调整大小(按比例),
我在互联网上搜索了一些代码,并在 StackOverFlow 中找到了这个答案 https://stackoverflow.com/a/6501997/3264464
static public Bitmap ScaleImage(Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
Bitmap bmp = new Bitmap(newImage);
return bmp;
}
我为我的代码添加了函数,但不确定 MaxHeight,MaxWidth 的东西,我的意思是为什么我需要通过参数发送它
我在Form1_Resize 事件处理程序中写道:
private void Form1_Resize(object sender, EventArgs e)
{
Bitmap NewImg = ScaleImage(pictureBox1.Image, 1000, 1000);
pictureBox1.Image = NewImg;
}
但它不起作用..当我调整表单大小时没有任何反应
更新:尝试了所有结果都一样
看下面的图片,黑点是PictureBox的左边,不能动,你说的很好,但是我想要,图片左边在开始的时候一直在同一个点上
调整大小之前:
调整大小后
【问题讨论】:
-
Winforms?为什么不使用 Winforms 控件属性?
-
他的意思是右键单击一个控件并单击属性在那里你可以停靠图片框然后它会随着表单自动调整大小。
-
我假设这是winform并添加了标签,如果不是请添加相关标签
-
@SyedFarjadZiaZaidi 好的,我停靠了图片框,但它只在我调整表单大小时移动,它不会调整大小!