技术开发 频道

照片生成缩略图时的尺寸计算


【IT168技术文档】

  为了能保持住照片在缩小时不变形,所以需要重新计算缩略图的尺寸. 这里默认缩略图的尺寸的应该满足 长 >= 宽, 否则计算规则应有所变化.

  今天突然想明白了,记在这里以后备用.
/// <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; }
0
相关文章