0

ズームイン/ズームアウトし、画面を塗りつぶし、1:1 の比率を実行する Image コントロール クラスがあります (すべて 4 つの異なるボタンで制御されます。ただし、画面の解像度が 1280 x 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 の比率コントロールでは、右側にわずかな隙間ができますが、これは単なる空白です。提供されたヘルプは大歓迎です。私は画像制御に慣れていないので、これはかなり些細なことに思えるかもしれません。ごめんなさい

4

2 に答える 2

0

あなたの基本的な問題は、のスケーリング係数を計算するためのいくつかの奇妙な数学を持っていることだと思いますZoomFit。たとえば、画面のアスペクト比が1:1の場合、常に画像のアスペクト比に等しい倍率でズームします。つまり、2:1の画像は、実際の大きさに関係なく、常に200%拡大されます。

于 2012-12-03T23:42:43.093 に答える
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.Min の代わりに Math.Max を使用して比率を計算します。

于 2012-12-03T21:47:08.737 に答える