asp.net生成缩略图示例方法分享
更新时间:2021年9月22日 10:13 点击:1562
做站的时候经常会遇到要生成缩略图的功能,因为可能不同的情况需要用来不同大小的缩略图。
本文生成的图片都为正方形,只有正方形的缩略图才是保证图片足够清晰。
当我我这里说的正方形是先按比例压缩,然后加一个固定的白底 然后居中显示。
代码:
新建outputimg.ashx
复制代码 代码如下:
//调整图片大小
private static Size NewSize(int maxWidth, int maxHeight, int Width, int Height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(Width);
double sh = Convert.ToDouble(Height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)//如果maxWidth和maxHeight大于源图像,则缩略图的长和高不变
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
复制代码 代码如下:
//生成缩略图
public static void SendSmallImage(string filename, string newfile, int maxHeight, int maxWidth, string mode)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(filename);//源图像的信息
System.Drawing.Imaging.ImageFormat thisformat = img.RawFormat; //源图像的格式
Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height); //返回调整后的图像Width与Height
Bitmap outBmp = new Bitmap(maxWidth, maxHeight);
Graphics g = Graphics.FromImage(outBmp);
//设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Color.White);
g.DrawImage(img, new Rectangle(((maxWidth - newSize.Width) / 2), ((maxHeight - newSize.Height) / 2), newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获取包含有关内置图像编码解码器的信息的ImageCodecInfo对象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//设置jpeg编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(newfile, jpegICI, encoderParams);
}
else
{
outBmp.Save(newfile, thisformat);
}
img.Dispose();
outBmp.Dispose();
}
输出图片:
复制代码 代码如下:
//输出图片
public static void OutPutImg(string imgFilePath)
{
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(imgFilePath), FileMode.Open, FileAccess.Read);
DateTime contentModified = System.IO.File.GetLastWriteTime(HttpContext.Current.Server.MapPath(imgFilePath));
if (IsClientCached(contentModified))
{
HttpContext.Current.Response.StatusCode = 304;
HttpContext.Current.Response.SuppressContent = true;
}
else
{
byte[] mydata = new byte[fs.Length];
int Length = Convert.ToInt32(fs.Length);
fs.Read(mydata, 0, Length);
fs.Close();
HttpContext.Current.Response.OutputStream.Write(mydata, 0, Length);
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.End();
HttpContext.Current.Response.Cache.SetETagFromFileDependencies();
HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(true);
HttpContext.Current.Response.Cache.SetLastModified(contentModified);
}
}
复制代码 代码如下:
//outpuimg.ashx?src=/images/weimeidesc/8af30049-797e-4eb4-8a54-cc4de47c1694.jpg!100x100.jpg
public void ProcessRequest(HttpContext context)
{
//获取图片
string imgUrl = context.Request.QueryString["src"];
string trueFilePath = imgUrl.Split('!')[0];
//获取图片大小
int width = Convert.ToInt32(imgUrl.Split('!')[1].Replace(".jpg", "").Split('x')[0]);
int height = Convert.ToInt32(imgUrl.Split('!')[1].Replace(".jpg", "").Split('x')[1]);
//图片已经存在直接输出
if (File.Exists(context.Server.MapPath("~/" + imgUrl)))
{
OutPutImg("~/"+imgUrl);
}
else
{
if (!string.IsNullOrEmpty(imgUrl) && File.Exists(context.Server.MapPath("~/" + trueFilePath)))
{
Image originalImage = System.Drawing.Image.FromFile(context.Server.MapPath("~/" + trueFilePath));
var newBitmap = new Bitmap(originalImage);
//生成相应的小图并保存
SendSmallImage(context.Server.MapPath("~/" + trueFilePath),context.Server.MapPath("~/" + imgUrl), width, height, "meiyouyisi");
//输出
OutPutImg("~/" + imgUrl);
}
else//图片如果不存在 输出默认图片
{
//OutPutImg(imgUrl);
}
}
}
相关文章
- if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
- 这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能哦,可以说是一款完美的图片上传类哦。 代码如下 复制代码 ...2016-11-25
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1...2016-11-25
- 这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印。 代码如下 复制代码 class upimages { var $annexf...2016-11-25
- 使用.net的方法GetThumbnailImage生成的缩略图失真严重,这里推荐一种不失真生成缩略图的方法...2021-09-22
- 等比例生成缩略图的php程序 这个程序很实现,但只是用来进来等比例生成缩略图哦,你要把文件上传到服务器,然后再由此函数来操作,有需要的朋友参考一下。 等比例生成缩...2016-11-25
- 一款实现的生成小图功能的实现代码,有需要的朋友可以参考,每个都有详细的说明参数。 代码如下 复制代码 <form action="uploads.php" method=...2016-11-25
- 本文章里面image就是你要生成的图片地址哦,这是一php按比例生成缩略图代码,只要给你图片他就能生成指定大小的图片哦,并且不变形ekt 代码如下 复制代码 ...2016-11-25
- 图片按在比较进行放大缩小,这得利用php教程 gd库的函数现实现,我们会利用到imagecreatetruecolor(),imagecopyresampled()来操作 function my_image_resize($src_file,...2016-11-25
- 网站大多数生成缩略图都是按比例操作的,今天我们来看看可以固定图片大小">图片大小,不够的地方自动填充空白哦。有需要的朋友可以参考一下。 <?php教程 error_report...2016-11-25
- 这篇文章主要介绍了ASP.NET实现上传图片并生成缩略图的方法,提供了一个asp.net上传图片与生成缩略图的方法,并给出了改进程序供大家对比分析,需要的朋友可以参考下...2021-09-22
- 本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/ 代码如下 复制代码 ...2016-11-25
- PHP令我们惊喜的就是在图形图象处理方面要忧于ASP,用GD库PHP就可以轻松的实现缩略图。这一篇文章我们的目的就是用GD来生成缩略图,PHP可以把缩略图直接生成输送到浏览器...2016-11-25
- <?php 代码如下 复制代码 if ($_GET['action'] == 'save') { $up = new upload(); $up->set_dir(dir...2016-11-25
- function ImageResize($srcFile,$toW,$toH,$toFile="") { if($toFile==""){ $toFile = $srcFile; } $info = ""; $data = GetImageSize($srcFile,$info); switch...2016-11-25
- 图片上传我们介绍过的教程非常的多了,今天我整理的这篇ajax图片上传主要有一个上传之后自动生成小图的功能并且还返回预览效果,下面我们来看看这段代码。 XML/HTML...2016-11-25
- c#生成缩略图的实现方法,需要的朋友可以参考一下...2020-06-25
- 本文给大家汇总了2个C#中等比缩放实现生成缩略图的方法,第一种稍微简单些,第二种是本人常用的方法,这里推荐给大家,有需要的小伙伴可以参考下。...2021-09-22
- 超简单php教程 大图生成缩略图实现代码 <?php /** * 生成缩略图 * * @param string $imagepath 图片路径 * @param string $thumb 生成缩略图名称 * @param i...2016-11-25
- 这篇文章主要介绍了C#实现的上传图片、保存图片、加水印、生成缩略图功能,结合实例形式较为详细的分析了C#图片上传、保存、水印、缩略图等相关操作技巧,需要的朋友可以参考下...2020-06-25