缩略图

无损缩略图生成
本文介绍了一种无损生成缩略图的方法,通过不同模式调整图片大小,并保持原始图片的比例,同时支持多种对齐方式。
ContractedBlock.gifExpandedBlockStart.gifView Code
/// <summary>
/// 缩略图
/// </summary>
/// <param name="srcImage">要缩略的图片</param>
/// <param name="thumSize">要缩放的尺寸</param>
/// <param name="mode">缩略模式</param>
/// <param name="alignment">对齐方式</param>
/// <returns>返回已经缩放的图片。</returns>
public static Bitmap GetDistortionlessThumbnailImage(Image srcImage, SizeF thumSize, ThumbnailMode mode, ContentAlignment alignment)
{
//先取宽比例。
float scale = (float)srcImage.Width / (float)thumSize.Width;
//缩略模式
switch (mode)
{
case ThumbnailMode.FullDistortionless:
if (srcImage.Height > srcImage.Width)
{
scale
= (float)srcImage.Height / (float)thumSize.Height;
}
break;
case ThumbnailMode.MaxDistortionless:
if (srcImage.Height / scale < thumSize.Height)
{
scale
= (float)srcImage.Height / (float)thumSize.Height;
}
break;
}
SizeF newSzie
= new SizeF(srcImage.Width / scale, srcImage.Height / scale);
Bitmap result
= new Bitmap((int)thumSize.Width, (int)thumSize.Height);
using (Graphics g = Graphics.FromImage(result))
{
//背景颜色
g.FillRectangle(Brushes.White, new RectangleF(PointF.Empty, thumSize));
g.InterpolationMode
= InterpolationMode.HighQualityBicubic;
g.SmoothingMode
= SmoothingMode.HighQuality;
g.PixelOffsetMode
= PixelOffsetMode.HighQuality;
g.CompositingMode
= CompositingMode.SourceOver;
g.CompositingQuality
= CompositingQuality.HighQuality;
g.TextRenderingHint
= TextRenderingHint.AntiAliasGridFit;
//对齐方式
RectangleF destRect;
switch (alignment)
{
case ContentAlignment.TopCenter:
destRect
= new RectangleF(new PointF(-((newSzie.Width - thumSize.Width) / 2), 0), newSzie);
break;
case ContentAlignment.TopRight:
destRect
= new RectangleF(new PointF(-(newSzie.Width - thumSize.Width), 0), newSzie);
break;
case ContentAlignment.MiddleLeft:
destRect
= new RectangleF(new PointF(0, -((newSzie.Height - thumSize.Height) / 2)), newSzie);
break;
case ContentAlignment.MiddleCenter:
destRect
= new RectangleF(new PointF(-((newSzie.Width - thumSize.Width) / 2), -((newSzie.Height - thumSize.Height) / 2)), newSzie);
break;
case ContentAlignment.MiddleRight:
destRect
= new RectangleF(new PointF(-(newSzie.Width - thumSize.Width), -((newSzie.Height - thumSize.Height) / 2)), newSzie);
break;
case ContentAlignment.BottomLeft:
destRect
= new RectangleF(new PointF(0, -(newSzie.Height - thumSize.Height)), newSzie);
break;
case ContentAlignment.BottomCenter:
destRect
= new RectangleF(new PointF(-((newSzie.Width - thumSize.Width) / 2), -(newSzie.Height - thumSize.Height)), newSzie);
break;
case ContentAlignment.BottomRight:
destRect
= new RectangleF(new PointF(-(newSzie.Width - thumSize.Width), -(newSzie.Height - thumSize.Height)), newSzie);
break;
default:
destRect
= new RectangleF(PointF.Empty, newSzie);
break;
}
g.DrawImage(srcImage, destRect,
new RectangleF(PointF.Empty, srcImage.Size), GraphicsUnit.Pixel);
g.Dispose();
}
return result;
}

转载于:https://www.cnblogs.com/Googler/archive/2011/04/07/2008260.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值