C# 调用FitToScreen()时,PictureBox的大小随着输入的相同而增大
我的程序中的某些功能有问题。它应该使图像适合屏幕,但随后调用此函数会导致图像的大小略微增加。在发出大约五次命令后,情况变得明显。下面是该错误的示例: 此程序的源是托管的。确保您在issue1分支中,因为它专门针对此问题。它是独立的;下载、编译、运行。它还包括一个测试漫画使用 FitToScreen功能存储在MainWindow.cs中: 调用Width=\u comicController.FitComicViewToWindowframeHeight存储在comicController.cs中,允许根据给定的高度以正确的纵横比显示漫画,并返回要使用的窗口宽度:C# 调用FitToScreen()时,PictureBox的大小随着输入的相同而增大,c#,.net,layout,picturebox,C#,.net,Layout,Picturebox,我的程序中的某些功能有问题。它应该使图像适合屏幕,但随后调用此函数会导致图像的大小略微增加。在发出大约五次命令后,情况变得明显。下面是该错误的示例: 此程序的源是托管的。确保您在issue1分支中,因为它专门针对此问题。它是独立的;下载、编译、运行。它还包括一个测试漫画使用 FitToScreen功能存储在MainWindow.cs中: 调用Width=\u comicController.FitComicViewToWindowframeHeight存储在comicController.cs中
public int FitComicViewToWindow(int frameHeight)
{
//Take into account in margins around the ComicView
var marginedHeight = frameHeight - ComicView.Margin.Vertical;
//Return the image's width if its height is smaller than the windowHeight
if (marginedHeight >= _comic.CurrentPage.Height /* <-- Law of Demeter?*/) return _comic.CurrentPage.Width;
//Calculate the aspect ratio
var aspectedWidth = marginedHeight * _comic.CurrentPage.Width / _comic.CurrentPage.Height;
//Make sure that the actual page fills the inside of the picturebox when it resizes
ComicView.SizeMode = PictureBoxSizeMode.StretchImage;
ComicView.Size = new Size(aspectedWidth, marginedHeight);
//return the aspected width so the window can resize
return aspectedWidth;
}
这就是它的工作原理!需要注意的一些事项:
显示漫画的picturebox在初始化期间添加到TableLayoutPanel。存储picturebox的TLP行的高度是用于计算的高度,即使它本身未被修改。单步执行调试器显示,每调用一次函数,此行的高度都会增加1个像素
任何帮助都将不胜感激,请让我知道你的想法 答案比我想象的要简单;获取帧高时,必须记住再删除1个像素。这给出了包含picturebox的表布局面板行的内部高度。对于我的实现,在将帧高度传递到Width=\u comicController.FitComicViewToWindowframeHeight调用之前,我删除了这个像素
FitToScreen现在可以产生可预测的结果。是否设置了边距、填充或边框?您尝试过计算纵横比的数学.Round吗?@TaW在_comicController.FitComicViewToWindowframeHeight调用中,我使用var marginedHeight=frameHeight-ComicView.Margin.vertical考虑了picturebox的垂直边距。也许这与没有考虑到水平边距有关?对不起,我把答案贴错了!嗯,听起来不太可能;宽度保持不变?使用横向图像时会发生什么情况?_漫画.CurrentPage.Width和_漫画.CurrentPage.Height的类型是什么?如果它们是int,则截断var aspectedWidth=marginedHeight*\u comic.CurrentPage.Width/\u comic.CurrentPage.Height中的除法和乘法值;尝试将一个值强制转换为浮点值,如var aspectedWidth=marginedHeight*\u comic.CurrentPage.Width/float\u comic.CurrentPage.Height;
public int FitComicViewToWindow(int frameHeight)
{
//Take into account in margins around the ComicView
var marginedHeight = frameHeight - ComicView.Margin.Vertical;
//Return the image's width if its height is smaller than the windowHeight
if (marginedHeight >= _comic.CurrentPage.Height /* <-- Law of Demeter?*/) return _comic.CurrentPage.Width;
//Calculate the aspect ratio
var aspectedWidth = marginedHeight * _comic.CurrentPage.Width / _comic.CurrentPage.Height;
//Make sure that the actual page fills the inside of the picturebox when it resizes
ComicView.SizeMode = PictureBoxSizeMode.StretchImage;
ComicView.Size = new Size(aspectedWidth, marginedHeight);
//return the aspected width so the window can resize
return aspectedWidth;
}