sizeprocessor.cs

来自「POCKET PC,照片管理系统!提供了真正意义上的目录打开功能」· CS 代码 · 共 53 行

CS
53
字号
using System;
using System.Drawing;

namespace Addot.Drawing
{
	/// <summary>
	/// Implements some processing about sizes.
	/// </summary>
	public class SizeProcessor
	{
        /// <summary>
        /// Process the size of 2 graphic elements to center the second one within the first one.
        /// </summary>
        /// <param name="container">The size of the container graphic element.</param>
        /// <param name="contained">The size of the contained graphic element.</param>
        /// <returns>The position of the contained graphic element relative to the container.</returns>
        public static Point CenterToContainer(Size container, Size contained)
        {
            Point topLeft = new Point();
            topLeft.X = (container.Width - contained.Width) / 2;
            topLeft.Y = (container.Height - contained.Height) / 2;
            return topLeft;
        }

        /// <summary>
        /// Process sizes and position of 2 graphic elements to resize the second one for being
        /// fitted into the first one. This method maintains proportions.
        /// </summary>
        /// <param name="container">The size of the container graphic element.</param>
        /// <param name="contained">The size of the contained graphic element, modified to fit the container size.</param>
        /// <param name="position">The new position of the contained element.</param>
        public static void FitToContainer(Size container, ref Size contained, ref Point position)
        {
            double height = contained.Height + double.Epsilon;
            double width = contained.Width + double.Epsilon;
            double ratio = width / height;

            if( contained.Width > contained.Height )
            {
                contained.Width = container.Width;
                contained.Height = (int)(container.Width / ratio);
            }
            else
            {
                contained.Height = container.Height;
                contained.Width = (int)(container.Height * ratio);
            }

            position = SizeProcessor.CenterToContainer(container, contained);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?