📄 sizeprocessor.cs
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -