内切图片
/// <summary>
/// 内切正方形图片算法
/// </summary>
/// <param name="b">原始图片</param>
/// <returns>图片内切区域</returns>
public System.Drawing.RectangleF Zoom1Image(System.Drawing.Image b)
{
System.Drawing.PointF pt = new System.Drawing.PointF(0, 0);
System.Drawing.SizeF size = new System.Drawing.SizeF(b.Width, b.Height);
//float rate = 1f;
if (b.Width <= b.Height)
{
//rate = smallw / b.Width;
//size.Height = b.Height * rate;
//pt.Y = (b.Height - size.Height) / 2f;
size.Height = b.Width;
pt.Y = (b.Height - b.Width) / 2;
}
else
{//高度全部
size.Width = b.Height;
//rate = smallw / b.Height;
//size.Width = b.Width * rate;
pt.X = (b.Width - b.Height) / 2f;
}
return new System.Drawing.RectangleF(pt, size);
}
/// <summary>
/// 图片外切缩放
/// </summary>
/// <param name="b"></param>
/// <returns>正方形内图片所占区域</returns>
public System.Drawing.RectangleF Zoom2Image(System.Drawing.Image b)
{
float smallw = 200;//图片宽度
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0,0,smallw,smallw);
if (b.Width > b.Height)
{
float rate = smallw / b.Width;
float height = rate * b.Height;
float py = (smallw - height)/2;
rect = new System.Drawing.RectangleF(0, py, smallw, height);
}
else
{
float rate = smallw / b.Height;
float width = rate * b.Width;
float px = (smallw - width) / 2;
rect = new System.Drawing.RectangleF(px, 0, width, smallw);
}
return rect;
}
/// <summary>
/// 等比例缩放
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public System.Drawing.SizeF ZoomImage(System.Drawing.Image b)
{
var s = new System.Drawing.SizeF(b.Width, b.Height);
if (b.Width > standW || b.Height > standW)
{
float rate = 1f;
float wr = standW / b.Width;
float hr = standW / b.Height;
float width = b.Width;
float height = b.Height;
if (wr >= hr)
{
rate = wr;
}
else
{
rate = hr;
}
width = b.Width * rate;
height = b.Height * rate;
}
return s;
}