c# - resizing pictureBox proportionally to Form resizing -
i want everytime , user resizes form , image in picturebox , resizes same values ( proportionally ) ,
i searched on internet codes , found answer in 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; }
i added function code , , not sure maxheight,maxwidth thing , mean why need send via parameters
and in form1_resize
event handler wrote:
private void form1_resize(object sender, eventargs e) { bitmap newimg = scaleimage(picturebox1.image, 1000, 1000); picturebox1.image = newimg; }
but won't work .. nothing happens when resize form
update: tried same results
look @ pictures below , black point left of picturebox , must not move , suggested , want , left of pictures stays on same point @ beggining
before resize:
after resize
in winforms can use picturebox properties this, without code:
add picture box , go control properties
this gives 5 choices. effect of of same image on winform below:
- normal shows image , fits (i believe pixel 0,0) @ no scaling or moving.
- stretchimage scale , force fit of image, if means skewing image
- autosize in case shows full image, in case image bigger form why there huge blue chunk.
- center image leaves control intact , shows center region of image
- zoom zooms in or out of image shows in entirety in picturebox control.
use in combination anchor or dock of control keep control want it, , scale image.
it sounds might want zoom.
zoom in action, anchor points set sides (and group box added simulate control box:
i can resize form, in case maximized , picturebox control takes care of out additional code:
note, because using rectangular image have set gray background show control versus image. set control color background make less obvious, or use stretchimage instead of zoom force image fill control, although creates plenty of ugly artifacts on non-square images.
example of stretched artifacts:
Comments
Post a Comment