调用 FitToScreen() 时,PictureBox 的大小会随着相同的输入而增加
PictureBox increases in size with the same inputs when calling FitToScreen()
我的程序中的某些功能有问题。它应该使图像适合屏幕,它确实这样做了,但是对该函数的后续调用导致图像的尺寸稍微增加。在发出大约五次命令后,它变得很明显。这是错误的示例 gif:
此程序的源代码托管在 here。确保您在 "issue1" 分支中,因为它专门针对此问题。它是独立的;下载、编译、运行。它还包括一个测试 "comic" 以供使用。
FitToScreen()
功能存储在 MainWindow.cs:
private void FitToScreen()
{
//TODO: frameHeight (aka Height of picture box's tablelayout cell) keeps growing with subsequent calls...
// ensure a comic has been loaded
if (!_comicController.ComicLoaded())
{
MessageBox.Show("No comic loaded!", "CinemaStrips");
return;
}
// make the window's height as tall as screen
var screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
Height = screenHeight;
var frameHeight = tableLayoutPanel1.GetRowHeights()[1];
// send the comicController the size it needs to try and fit into
Width = _comicController.FitComicViewToWindow(frameHeight);
// put window location at 0 height and center width of screen
Top = 0;
var screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Left = (screenWidth - Width)/2;
}
对 Width = _comicController.FitComicViewToWindow(frameHeight)
的调用存储在 ComicController.cs 中,并允许根据给定的高度以正确的纵横比显示漫画, returns window 使用的宽度:
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;
}
这就是它的工作原理! 一些注意事项:
显示漫画的图片框在初始化期间添加到 TableLayoutPanel。存储图片框的 TLP 行的高度用于计算,即使它本身没有被修改。 单步执行调试器显示该行的高度在每次函数调用时增加 1 个像素。
如有任何帮助,我们将不胜感激,请告诉我您的想法!
答案比我想的要简单;当得到 frameHeight
时,一定要记住再移除 1 个像素。这给出了包含我们图片框的 table 布局面板行的 "inner" 高度。对于我的实现,我在将 frameHeight 传递到 Width = _comicController.FitComicViewToWindow(frameHeight)
调用之前删除了这个像素。
FitToScreen() 现在生成预测table 结果。
我的程序中的某些功能有问题。它应该使图像适合屏幕,它确实这样做了,但是对该函数的后续调用导致图像的尺寸稍微增加。在发出大约五次命令后,它变得很明显。这是错误的示例 gif:
此程序的源代码托管在 here。确保您在 "issue1" 分支中,因为它专门针对此问题。它是独立的;下载、编译、运行。它还包括一个测试 "comic" 以供使用。
FitToScreen()
功能存储在 MainWindow.cs:
private void FitToScreen()
{
//TODO: frameHeight (aka Height of picture box's tablelayout cell) keeps growing with subsequent calls...
// ensure a comic has been loaded
if (!_comicController.ComicLoaded())
{
MessageBox.Show("No comic loaded!", "CinemaStrips");
return;
}
// make the window's height as tall as screen
var screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
Height = screenHeight;
var frameHeight = tableLayoutPanel1.GetRowHeights()[1];
// send the comicController the size it needs to try and fit into
Width = _comicController.FitComicViewToWindow(frameHeight);
// put window location at 0 height and center width of screen
Top = 0;
var screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Left = (screenWidth - Width)/2;
}
对 Width = _comicController.FitComicViewToWindow(frameHeight)
的调用存储在 ComicController.cs 中,并允许根据给定的高度以正确的纵横比显示漫画, returns window 使用的宽度:
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;
}
这就是它的工作原理! 一些注意事项: 显示漫画的图片框在初始化期间添加到 TableLayoutPanel。存储图片框的 TLP 行的高度用于计算,即使它本身没有被修改。 单步执行调试器显示该行的高度在每次函数调用时增加 1 个像素。
如有任何帮助,我们将不胜感激,请告诉我您的想法!
答案比我想的要简单;当得到 frameHeight
时,一定要记住再移除 1 个像素。这给出了包含我们图片框的 table 布局面板行的 "inner" 高度。对于我的实现,我在将 frameHeight 传递到 Width = _comicController.FitComicViewToWindow(frameHeight)
调用之前删除了这个像素。
FitToScreen() 现在生成预测table 结果。