//ViewSize 外框大小
//ImageSize 图片的实际大小
public Size Resize(Size ViewSize, Size ImageSize)
{
Size MySize = new Size();
if(ViewSize.Width > ViewSize.Height)//宽大于高
{
if(ImageSize.Width > ImageSize.Height)//按比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
if(ViewSize.Height / (float)ViewSize.Width < scale)
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height / scale);
}
else
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
}
else if(ImageSize.Height > ImageSize.Width)//非比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else//正方
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
else if(ViewSize.Width < ViewSize.Height)//高大于宽
{
if(ImageSize.Width < ImageSize.Height)//按比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
if(ViewSize.Width / (float)ViewSize.Height < scale)
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width / scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
}
else if(ImageSize.Height < ImageSize.Width)//非比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else//正方
{
MySize.Height = ViewSize.Width;
MySize.Width = ViewSize.Width;
}
}
else//正方
{
if(ImageSize.Width > ImageSize.Height)//宽大于高
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else if(ImageSize.Width < ImageSize.Height)//高大于宽
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else//正方
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
return MySize;
}
//ImageSize 图片的实际大小
public Size Resize(Size ViewSize, Size ImageSize)
{
Size MySize = new Size();
if(ViewSize.Width > ViewSize.Height)//宽大于高
{
if(ImageSize.Width > ImageSize.Height)//按比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
if(ViewSize.Height / (float)ViewSize.Width < scale)
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height / scale);
}
else
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
}
else if(ImageSize.Height > ImageSize.Width)//非比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else//正方
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
else if(ViewSize.Width < ViewSize.Height)//高大于宽
{
if(ImageSize.Width < ImageSize.Height)//按比例
{
float scale = ImageSize.Width / (float)ImageSize.Height;
if(ViewSize.Width / (float)ViewSize.Height < scale)
{
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width / scale);
}
else
{
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
}
else if(ImageSize.Height < ImageSize.Width)//非比例
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else//正方
{
MySize.Height = ViewSize.Width;
MySize.Width = ViewSize.Width;
}
}
else//正方
{
if(ImageSize.Width > ImageSize.Height)//宽大于高
{
float scale = ImageSize.Height / (float)ImageSize.Width;
MySize.Width = ViewSize.Width;
MySize.Height = (int)(ViewSize.Width * scale);
}
else if(ImageSize.Width < ImageSize.Height)//高大于宽
{
float scale = ImageSize.Width / (float)ImageSize.Height;
MySize.Height = ViewSize.Height;
MySize.Width = (int)(ViewSize.Height * scale);
}
else//正方
{
MySize.Height = ViewSize.Height;
MySize.Width = ViewSize.Height;
}
}
return MySize;
}