c# - PictureBox increases in size with the same inputs when calling FitToScreen() -
i’m having issue functionality in program. supposed fit image screen, does, subsequent calls function cause image increase in size ever slightly. becomes apparent after issuing command 5 times. here’s example gif of bug:
the source program hosted here. make sure in "issue1" branch, geared toward issue. self-contained; download, compile, run. includes test "comic" use.
the fittoscreen()
functionality stored in mainwindow.cs:
private void fittoscreen() { //todo: frameheight (aka height of picture box's tablelayout cell) keeps growing subsequent calls... // ensure comic has been loaded if (!_comiccontroller.comicloaded()) { messagebox.show("no comic loaded!", "cinemastrips"); return; } // make window's height tall screen var screenheight = screen.primaryscreen.workingarea.height; height = screenheight; var frameheight = tablelayoutpanel1.getrowheights()[1]; // send comiccontroller size needs try , fit width = _comiccontroller.fitcomicviewtowindow(frameheight); // put window location @ 0 height , center width of screen top = 0; var screenwidth = screen.primaryscreen.workingarea.width; left = (screenwidth - width)/2; }
the call width = _comiccontroller.fitcomicviewtowindow(frameheight)
stored in comiccontroller.cs , allows comic displayed correct aspect ratio, based on height given, , returns width window use:
public int fitcomicviewtowindow(int frameheight) { //take account in margins around comicview var marginedheight = frameheight - comicview.margin.vertical; //return image's width if height smaller windowheight if (marginedheight >= _comic.currentpage.height /* <-- law of demeter?*/) return _comic.currentpage.width; //calculate aspect ratio var aspectedwidth = marginedheight * _comic.currentpage.width / _comic.currentpage.height; //make sure actual page fills inside of picturebox when resizes comicview.sizemode = pictureboxsizemode.stretchimage; comicview.size = new size(aspectedwidth, marginedheight); //return aspected width window can resize return aspectedwidth; }
and that's how works! some things note: picturebox displays comic added tablelayoutpanel during initialization. tlp row's height stores picturebox used calculations, though of not modified. stepping through debugger shows row's height increases 1 pixel every function call.
any appreciated , please let me know think!
the answer turned out simpler realized; when getting frameheight
, 1 must remember remove additional 1 pixel. gives "inner" height of table layout panel's row containing our picturebox. implementation, removed pixel before passing frameheight width = _comiccontroller.fitcomicviewtowindow(frameheight)
call.
the fittoscreen() produces predictable results.