Example #1
0
        public bool Scale(ScaleType scaleType, PositionType positionType, AspectRatio aspectRatio, Vector2 padding)
        {
            if (!_rs232Device.Enabled)
            {
                return(false);
            }

            if (padding == null)
            {
                padding = new Vector2();
            }

            var outputEdid = GetOutputRate();

            int inputWidth  = (int)GetActivePixels();
            int inputHeight = (int)GetActiveLines();

            int inputWidthWithoutPadding  = inputWidth - (int)Math.Round(padding.X * 2);
            int inputHeightWithoutPadding = inputHeight - (int)Math.Round(padding.Y * 2);

            if (aspectRatio != AspectRatio.RatioPreserve)
            {
                inputHeightWithoutPadding = (int)(inputWidthWithoutPadding * (1.0f / aspectRatio.GetRatio()));
                inputHeight = (int)(inputWidth * (1.0f / aspectRatio.GetRatio()));
            }

            float edidRatio  = (float)outputEdid.Width / (float)outputEdid.Height;
            float inputRatio = (float)inputWidth / (float)inputHeight;

            int vSize = 0;
            int hSize = 0;
            int vPos  = 0;
            int hPos  = 0;

            switch (scaleType)
            {
            case ScaleType.PixelPerfect:
                vSize = inputHeight;
                hSize = inputWidth;
                break;

            case ScaleType.Fit:
                if (inputHeight > inputWidth)     //Portrait
                {
                    vSize = outputEdid.Height;
                    hSize = (int)(vSize * inputRatio);

                    if (hSize > outputEdid.Width)
                    {
                        float scale = (float)outputEdid.Width / (float)hSize;
                        hSize = (int)((float)hSize * scale);
                        vSize = (int)((float)vSize * scale);
                    }
                }
                else     //Landscape or Square
                {
                    hSize = outputEdid.Width;
                    vSize = (int)(hSize * (1 / inputRatio));

                    if (vSize > outputEdid.Height)
                    {
                        float scale = (float)outputEdid.Height / (float)vSize;
                        hSize = (int)((float)hSize * scale);
                        vSize = (int)((float)vSize * scale);
                    }
                }
                break;

            case ScaleType.FitWidth:
            {
                var widthPaddingScale = (float)inputWidth / inputWidthWithoutPadding;

                hSize = (int)(outputEdid.Width * (float)widthPaddingScale);
                vSize = (int)(hSize * (1 / inputRatio));
            }
            break;
            }

            switch (positionType)
            {
            case PositionType.Centre:
                hPos = ((outputEdid.Width - hSize) / 2);
                vPos = ((outputEdid.Height - vSize) / 2);
                break;
            }

            var result = SetImagePositionAndSize(new PositionAndSize(hPos, vPos, hSize, vSize));

            return(result);
        }