asp.net 生成GIF格式的缩略图
在开发中遇到一个生成缩略图的问题:怎样把一张会动的GIF格式的图片生成缩略图,生成的缩略图也要会动
以下是我所用到的生成缩略图的方法,希望各位高手帮忙解决一下,小弟不胜感激!!!
这个方法对jpg有效,GIF就无效了
//生成缩略图
//originalImagePath: 存放图片的原始路径
//thumbnailPath : 生成缩略图的路径
//width: 宽
//height: 高
//type: 这里的type是用于接收图片的类型(我这里上传是只接收JPG和GIF两种格式)
//mode: 生成图片模型
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string type, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
if (type == "jpg" || type == "JPG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
以下是我所用到的生成缩略图的方法,希望各位高手帮忙解决一下,小弟不胜感激!!!
这个方法对jpg有效,GIF就无效了
//生成缩略图
//originalImagePath: 存放图片的原始路径
//thumbnailPath : 生成缩略图的路径
//width: 宽
//height: 高
//type: 这里的type是用于接收图片的类型(我这里上传是只接收JPG和GIF两种格式)
//mode: 生成图片模型
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string type, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
if (type == "jpg" || type == "JPG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
...全文
相关推荐
ASP.NET Gif生成略缩图
ASP.NET是一种基于.NET Framework的服务器端编程模型,用于构建动态Web应用程序。在本文中,我们将深入探讨如何在ASP.NET环境中实现Gif动画的缩略图生成功能。
用ASP.Net生成缩略图!!
最后,将缩略图保存到服务器的另一个路径,比如 "smallpic/rs1.gif",并释放原始图片资源以避免内存泄漏。这个简单的示例展示了 ASP.NET 如何通过内建功能实现缩略图的生成。
ASP.NET加水印文字并生成缩略图
该项目基于ASP.NET实现图片上传、生成缩略图及添加自定义文字水印功能。支持JPEG和GIF格式图片,上传后按时间戳重命名并保存,使用DrawHelper类进行等比缩放压缩,最终生成带水印的JPEG
图片上传源码,供大家分享
本文介绍了一个ASP.NET网页应用,实现了图片上传、生成缩略图和水印图片的功能。支持BMP、GIF和JPEG格式,上传后保存原图并生成带文字或图片水印的图片及缩略图。还包括自动下载网页图片保存到服务
Asp.net上传图片同时生成缩略图和水印图
在ASP.NET中,上传图片并生成缩略图和水印图是一项常见的需求,特别是在创建网站或者应用程序时,需要处理用户上传的图像。以下是一个关于如何在ASP.NET中实现这个功能的详细说明。


