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:

before

after resize

enter image description here

in winforms can use picturebox properties this, without code:

add picture box , go control properties

enter image description here

this gives 5 choices. effect of of same image on winform below:

enter image description here

  • 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:

enter image description here

i can resize form, in case maximized , picturebox control takes care of out additional code:

enter image description here

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:

enter image description here


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -