2012-12-03 135 views
0

我有一個圖像控制類,放大/縮小,填充屏幕,並執行1:1的比例(全部由4個不同的按鈕控制),但是當屏幕分辨率大於1280 ×1024,我的填充屏幕和1:1的比例不這樣做,他們都應該縮放和圖像填充屏幕

public void ZoomActual() 
    { 
     m_translateTransform.BeginAnimation(TranslateTransform.XProperty, CreateAnimation(0)); 
     m_translateTransform.BeginAnimation(TranslateTransform.YProperty, CreateAnimation(0)); 
     m_zoomFactor.BeginAnimation(ScaleTransform.ScaleXProperty, CreateAnimation(1)); 
     m_zoomFactor.BeginAnimation(ScaleTransform.ScaleYProperty, CreateAnimation(1)); 
    } //1:1 ratio button control 

    /// <summary> 
    /// This function is used to fill the screen with the current picture on the zoomandpan Control 
    /// </summary> 
    public void ZoomFit() 
    { 
     double screen_height = ActualHeight; 
     double screen_width = ActualWidth; 

     double image_width = m_source_child.ActualWidth; 
     double image_height = m_source_child.ActualHeight; 

     double image_ratio = image_width/image_height; 
     double screen_ratio = screen_width/screen_height; 

     if (image_width > image_height) 
     { 
      m_translateTransform.BeginAnimation(TranslateTransform.XProperty, CreateAnimation(0)); 
      m_translateTransform.BeginAnimation(TranslateTransform.YProperty, CreateAnimation(0)); 
      m_zoomFactor.BeginAnimation(ScaleTransform.ScaleXProperty, CreateAnimation(1/screen_ratio * image_ratio)); //width 
      m_zoomFactor.BeginAnimation(ScaleTransform.ScaleYProperty, CreateAnimation(1/screen_ratio * image_ratio)); //height 
     } 
     else 
     { 
      m_translateTransform.BeginAnimation(TranslateTransform.XProperty, CreateAnimation(0)); 
      m_translateTransform.BeginAnimation(TranslateTransform.YProperty, CreateAnimation(0)); 
      m_zoomFactor.BeginAnimation(ScaleTransform.ScaleXProperty, CreateAnimation((screen_ratio * image_ratio))); //width 
      m_zoomFactor.BeginAnimation(ScaleTransform.ScaleYProperty, CreateAnimation((screen_ratio * image_ratio))); //height 
     } 
    } //fillscreen button control 

    /// <summary> 
    /// This function is used to control the animations of the ZoomandPan. Animations consist of Zooming in 
    /// and out of a picture and allows panning of the displayed image 
    /// </summary> 
    /// <param name="a_toValue">used for zoom in percentage [currently set at: 1.5 (150%)]</param> 
    /// <returns>Animation values image</returns> 
    private DoubleAnimation CreateAnimation(double a_toValue) 
    { 
     var dubAni = new DoubleAnimation(a_toValue, new Duration(TimeSpan.FromMilliseconds(300))) 
     { 
      AccelerationRatio = 0.1, 
      DecelerationRatio = 0.9, 
      FillBehavior = FillBehavior.HoldEnd 
     }; 
     dubAni.Freeze(); 
     return dubAni; 
    } //Animation value setter 

    /// <summary> 
    /// DoZoom is used for executing the physical zoom of the picture, enlarges or shrinks image depending 
    /// on CreateAnimation values 
    /// </summary> 
    /// <param name="a_deltaZoom">Determinded to be + or -. can be set by mousewheel and/or buttons. Determines Zoom in/out</param> 
    /// <param name="a_mousePosition">Current positon of mouse</param> 
    /// <param name="a_physicalPositon">refrence to last area mousePosition was on image</param> 
    private void DoZoom(Double a_deltaZoom, Point a_mousePosition, Point a_physicalPositon) 
    { 
     double currentZoom = m_zoomFactor.ScaleX; 
     currentZoom *= a_deltaZoom; 
     if (currentZoom < MinZoom) 
      currentZoom = MinZoom; 
     else if (currentZoom > MaxZoom) 
      currentZoom = MaxZoom; 

     m_translateTransform.BeginAnimation(TranslateTransform.XProperty, CreateAnimation(-1 * (a_mousePosition.X * currentZoom - a_physicalPositon.X))); 
     m_translateTransform.BeginAnimation(TranslateTransform.YProperty, CreateAnimation(-1 * (a_mousePosition.Y * currentZoom - a_physicalPositon.Y))); 

     m_zoomFactor.BeginAnimation(ScaleTransform.ScaleXProperty, CreateAnimation(currentZoom)); 
     m_zoomFactor.BeginAnimation(ScaleTransform.ScaleYProperty, CreateAnimation(currentZoom)); 
    } //Zoom animation 

當我這樣它不會做什麼應該我的意思是這樣的:如果我的屏幕的分辨率1280 x 1024或更低,填充屏幕會填滿屏幕,1:1比例會給出圖片的實際尺寸,任何大於1280 x 1024的分辨率都會導致填充屏幕控制使圖像變小(在內部畫布),而不是將整個圖像放在窗口上,並且採用1:1的比例控制將有一個小的差距,只是空白的權利。提供的任何幫助非常感謝,我對圖像控制有點新,所以這可能看起來很瑣碎。對不起回合

+0

你的XAML是什麼樣的? 'Image'控件根據給定的空間和'Stretch'屬性來進行自己的擬合。 – RandomEngy

+0

MrBretten

+0

什麼是附加到m_zoomFactor和m_translateTransform故事板?請給出理解代碼所需的一切。 – RandomEngy

回答

0

我假設你在你的ZoomFit函數,你想填補,同時保持適當的長寬比。這是我通常如此做的:

double ratio = Math.Min(destArea.Width/imageSize.Width, destArea.Height/imageSize.Height); 

double imageWidth = imageSize.Width * ratio; 
double imageHeight = imageSize.Height * ratio; 

您應該能夠將比率直接提供給ScaleTransform。

如果要縮放以填充和保持縱橫比但修剪多餘部分,則只需使用Math.Max來計算比率而不是Math.Min。

+0

比率最小/最大工作! – MrBretten

0

我認爲你的基本問題是你有一些不可思議的數學來計算ZoomFit中的縮放因子。例如,如果您的屏幕寬高比爲1:1,則總是會縮放一個等於圖像寬高比的因子。這意味着2:1的圖像總是可以放大200%,不管它實際上有多大。