Zoom to Fit to canvas size in C#? -


i have canvas changes size depending on users input, want hit button , able zoom out see whole canvas. know height , width of canvas want zoom in or out user can see whole canvas. have zoom in , zoom out work not sure how zoom fit? thoughts?

february 11th

thank response, great , able zoom out on canvas completely. problem having user has zoom in, , zoom out controls. if user zooms in , out , tries zoom fit canvas scaling factor off, code below

this basic zooming in , out on canvas:

    double scalerate = 1.2;      public void buttonzoomin_click(object sender, routedeventargs e)     {         st.scalex *= scalerate;         st.scaley *= scalerate;     }      public void buttonzoomout_click(object sender, routedeventargs e)     {         st.scalex /= scalerate;         st.scaley /= scalerate;     } 

the zoom fit button want press zoom out on canvas:

private void zoomtofitbt_click(object sender, routedeventargs e)     {         float maxwidthscale = (float)scrollviewercanvas.width / (float)canvas1.width;         float maxheightscale = (float)scrollviewercanvas.height / (float)canvas1.height;         float scale = math.min(maxheightscale, maxwidthscale);          if (st.scalex > scale || st.scaley> scale)         {             st.scalex *= scale;             st.scaley *= scale;         }     } 

if press zoom fit button off start fine, when user has done manual zooming messes up. idea maybe every time user hits zoom fit button first go initial state use zoom fit code not sure that.

thanks guys

based on little information given, simplify problem gauge asking @ basic mathmatical level. think asking...

"i have 2 rectangles (the viewport, , canvas). how scale canvas such big possible without exceeding width or height of viewport."

this code determine how scale rectangle in such way barely fit inside of rectangle.

rectangle c = new rectangle(0, 0, 200, 100); //canvas rectancle (assume 200x100) rectangle v = new rectangle(0, 0, 50, 50); //viewport rectangle (assume 50x50)  //the maximum scale width use float maxwidthscale = (float)v.width / (float)c.width;   //the maximum scale height use float maxheightscale = (float)v.height / (float)c.height;   //use smaller of 2 scales scaling float scale = math.min(maxheightscale, maxwidthscale);  

scale = .25 (or 25%) in order fit using sample rectangles.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -