今天突然想明白了,记在这里以后备用.
///
<summary>
/// 取得缩略图的尺寸
/// </summary>
/// <param name="oldSize"> 原始尺寸 </param>
/// <param name="desiredSize"> 目标尺寸 </param>
/// <returns></returns>
public static Size GetSacaledSize(Size oldSize, Size desiredSize)
{
if (oldSize.Height == 0 || oldSize.Width == 0 )
throw new Exception( " The pic's width and height not set. " );
Size outSize = new Size();
bool flag = false ;
// 如果宽> 长,则按宽缩放 width = DesiredWidth.
// 如果 宽<= 长. 则按长缩放 height = DesiredHeight.
if (oldSize.Width <= desiredSize.Width && oldSize.Height <= desiredSize.Height)
{
outSize = oldSize;
flag = true ;
}
if ( ! flag)
{
if (oldSize.Width > oldSize.Height)
{
outSize.Width = desiredSize.Width;
double ratio = ( double )desiredSize.Width / oldSize.Width;
outSize.Height = Convert.ToInt32(oldSize.Height * ratio);
flag = true ;
}
}
if ( ! flag)
{
outSize.Height = desiredSize.Height;
double ratio = ( double )desiredSize.Height / oldSize.Height;
outSize.Width = Convert.ToInt32(oldSize.Width * ratio);
}
return outSize;
}
/// 取得缩略图的尺寸
/// </summary>
/// <param name="oldSize"> 原始尺寸 </param>
/// <param name="desiredSize"> 目标尺寸 </param>
/// <returns></returns>
public static Size GetSacaledSize(Size oldSize, Size desiredSize)
{
if (oldSize.Height == 0 || oldSize.Width == 0 )
throw new Exception( " The pic's width and height not set. " );
Size outSize = new Size();
bool flag = false ;
// 如果宽> 长,则按宽缩放 width = DesiredWidth.
// 如果 宽<= 长. 则按长缩放 height = DesiredHeight.
if (oldSize.Width <= desiredSize.Width && oldSize.Height <= desiredSize.Height)
{
outSize = oldSize;
flag = true ;
}
if ( ! flag)
{
if (oldSize.Width > oldSize.Height)
{
outSize.Width = desiredSize.Width;
double ratio = ( double )desiredSize.Width / oldSize.Width;
outSize.Height = Convert.ToInt32(oldSize.Height * ratio);
flag = true ;
}
}
if ( ! flag)
{
outSize.Height = desiredSize.Height;
double ratio = ( double )desiredSize.Height / oldSize.Height;
outSize.Width = Convert.ToInt32(oldSize.Width * ratio);
}
return outSize;
}
这里是单元测试代码:
///
<summary>
/// 这个测试用来测试在几种不同尺寸下面的缩略图尺寸算法的问题.
/// </summary>
[TestFixture]
public class PhotoCacheTestFixture
{
[Test]
public void CacheSizeTest()
{
Photos p = new Photos();
// 常见情况, 长大于宽的尺寸.
p.PhotoHeight = 1200 ;
p.PhotoWidth = 1600 ;
Size sizeSacled = p.SacledSize(ScaledPicSize.Instance.Size130x130);
Console.WriteLine( " 1600X1200 " );
Console.WriteLine(sizeSacled.Width);
Console.WriteLine(sizeSacled.Height);
Console.WriteLine( " ++++++++++++++++++ " );
Assert.IsTrue(sizeSacled.Width <= 130 );
Assert.IsTrue(sizeSacled.Height <= 130 );
// 原图比缩略图尺寸还小的情况.直接返回原尺寸.
p.PhotoWidth = 100 ;
p.PhotoHeight = 80 ;
Size size2 = p.SacledSize(ScaledPicSize.Instance.Size130x130);
Console.WriteLine( " 100X80 " );
Console.WriteLine(size2.Width);
Console.WriteLine(size2.Height);
Console.WriteLine( " ++++++++++++++++++ " );
Assert.IsTrue(size2.Width <= 130 );
Assert.IsTrue(size2.Height <= 130 );
// 长版的图片尺寸.
p.PhotoWidth = 80 ;
p.PhotoHeight = 140 ;
Size size3 = p.SacledSize(ScaledPicSize.Instance.Size130x130);
Console.WriteLine( " 80X140 " );
Console.WriteLine(size3.Width);
Console.WriteLine(size3.Height);
Console.WriteLine( " ++++++++++++++++++ " );
Assert.IsTrue(size3.Width <= 130 );
Assert.IsTrue(size3.Height <= 130 );
}
}
/// 这个测试用来测试在几种不同尺寸下面的缩略图尺寸算法的问题.
/// </summary>
[TestFixture]
public class PhotoCacheTestFixture
{
[Test]
public void CacheSizeTest()
{
Photos p = new Photos();
// 常见情况, 长大于宽的尺寸.
p.PhotoHeight = 1200 ;
p.PhotoWidth = 1600 ;
Size sizeSacled = p.SacledSize(ScaledPicSize.Instance.Size130x130);
Console.WriteLine( " 1600X1200 " );
Console.WriteLine(sizeSacled.Width);
Console.WriteLine(sizeSacled.Height);
Console.WriteLine( " ++++++++++++++++++ " );
Assert.IsTrue(sizeSacled.Width <= 130 );
Assert.IsTrue(sizeSacled.Height <= 130 );
// 原图比缩略图尺寸还小的情况.直接返回原尺寸.
p.PhotoWidth = 100 ;
p.PhotoHeight = 80 ;
Size size2 = p.SacledSize(ScaledPicSize.Instance.Size130x130);
Console.WriteLine( " 100X80 " );
Console.WriteLine(size2.Width);
Console.WriteLine(size2.Height);
Console.WriteLine( " ++++++++++++++++++ " );
Assert.IsTrue(size2.Width <= 130 );
Assert.IsTrue(size2.Height <= 130 );
// 长版的图片尺寸.
p.PhotoWidth = 80 ;
p.PhotoHeight = 140 ;
Size size3 = p.SacledSize(ScaledPicSize.Instance.Size130x130);
Console.WriteLine( " 80X140 " );
Console.WriteLine(size3.Width);
Console.WriteLine(size3.Height);
Console.WriteLine( " ++++++++++++++++++ " );
Assert.IsTrue(size3.Width <= 130 );
Assert.IsTrue(size3.Height <= 130 );
}
}