asp.net生成縮略圖示例方法分享
做站的時(shí)候經(jīng)常會(huì)遇到要生成縮略圖的功能,因?yàn)榭赡懿煌那闆r需要用來(lái)不同大小的縮略圖。
本文生成的圖片都為正方形,只有正方形的縮略圖才是保證圖片足夠清晰。
當(dāng)我我這里說(shuō)的正方形是先按比例壓縮,然后加一個(gè)固定的白底 然后居中顯示。
代碼:
新建outputimg.ashx
//調(diào)整圖片大小
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大于源圖像,則縮略圖的長(zhǎng)和高不變
{
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); //返回調(diào)整后的圖像Width與Height
Bitmap outBmp = new Bitmap(maxWidth, maxHeight);
Graphics g = Graphics.FromImage(outBmp);
//設(shè)置畫(huà)布的描繪質(zhì)量
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();
//以下代碼為保存圖片時(shí),設(shè)置壓縮質(zhì)量
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;
//獲取包含有關(guān)內(nèi)置圖像編碼解碼器的信息的ImageCodecInfo對(duì)象。
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];//設(shè)置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]);
//圖片已經(jīng)存在直接輸出
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);
//生成相應(yīng)的小圖并保存
SendSmallImage(context.Server.MapPath("~/" + trueFilePath),context.Server.MapPath("~/" + imgUrl), width, height, "meiyouyisi");
//輸出
OutPutImg("~/" + imgUrl);
}
else//圖片如果不存在 輸出默認(rèn)圖片
{
//OutPutImg(imgUrl);
}
}
}
- ASP.NET創(chuàng)建動(dòng)態(tài)縮略圖的方法
- asp.net中生成縮略圖并添加版權(quán)實(shí)例代碼
- asp.net生成縮略圖實(shí)現(xiàn)代碼
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net 生成縮略圖代碼
- asp.net 上傳圖片并同時(shí)生成縮略圖的代碼
- asp.net 點(diǎn)縮略圖彈出隨圖片大小自動(dòng)調(diào)整的頁(yè)面
- ASP.Net 上傳圖片并生成高清晰縮略圖
- asp.net生成高質(zhì)量縮略圖通用函數(shù)(c#代碼),支持多種生成方式
- ASP.NET中高質(zhì)量縮略圖的生成代碼
- asp.net圖片上傳生成縮略圖的注意事項(xiàng)
- ASP.NET實(shí)現(xiàn)根據(jù)URL生成網(wǎng)頁(yè)縮略圖的方法
相關(guān)文章
asp.net 票據(jù)簡(jiǎn)單應(yīng)用
asp.net票據(jù)應(yīng)用實(shí)例代碼。2009-03-03Repeater控件數(shù)據(jù)導(dǎo)出Excel(附演示動(dòng)畫(huà))
本文我們實(shí)現(xiàn)為Repeater控件數(shù)據(jù)導(dǎo)出Excel的功能,附動(dòng)畫(huà)演示,感興趣的朋友可以了解下2013-01-01.net實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼功能
這篇文章主要介紹了.net實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼功能,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04asp.net 動(dòng)態(tài)添加多個(gè)用戶控件
動(dòng)態(tài)添加多個(gè)相同用戶控件,并使每個(gè)用戶控件獲取不同的內(nèi)容。2009-12-12JS實(shí)現(xiàn)完美include加載功能代碼
在寫(xiě)這個(gè)之前在網(wǎng)上搜索了些資料,發(fā)現(xiàn)以前寫(xiě)的include都存在2個(gè)問(wèn)題,這也是include需要解決的比較重要的2個(gè)問(wèn)題。2010-10-10ASP.NET編程中經(jīng)常用到的27個(gè)函數(shù)集
asp.net 整理的27個(gè)函數(shù)集,大家可以參考下2008-08-08asp.net core 獲取 MacAddress 地址方法示例
這篇文章主要介紹了asp.net core獲取MacAddress地址方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02