ASP.NET中高質(zhì)量縮略圖的生成代碼
更新時(shí)間:2007年09月10日 21:50:34 作者:
private 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 )
{
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));
}
private void SendSmallImage(string fileName, int maxWidth, int maxHeight)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
// 設(shè)置畫(huà)布的描繪質(zhì)量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
if (thisFormat.Equals(ImageFormat.Gif))
{
Response.ContentType = "image/gif";
}
else
{
Response.ContentType = "image/jpeg";
}
// 以下代碼為保存圖片時(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(Response.OutputStream, jpegICI, encoderParams);
}
else
{
outBmp.Save(Response.OutputStream, thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
{
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 )
{
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));
}
private void SendSmallImage(string fileName, int maxWidth, int maxHeight)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(fileName));
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
// 設(shè)置畫(huà)布的描繪質(zhì)量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
if (thisFormat.Equals(ImageFormat.Gif))
{
Response.ContentType = "image/gif";
}
else
{
Response.ContentType = "image/jpeg";
}
// 以下代碼為保存圖片時(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(Response.OutputStream, jpegICI, encoderParams);
}
else
{
outBmp.Save(Response.OutputStream, thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
您可能感興趣的文章:
- ASP.NET創(chuàng)建動(dòng)態(tài)縮略圖的方法
- asp.net生成縮略圖示例方法分享
- 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圖片上傳生成縮略圖的注意事項(xiàng)
- ASP.NET實(shí)現(xiàn)根據(jù)URL生成網(wǎng)頁(yè)縮略圖的方法
相關(guān)文章
你應(yīng)該知道的.NET錯(cuò)誤與異常處理機(jī)制
這篇文章主要給大家介紹了關(guān)于.NET錯(cuò)誤與異常處理機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08asp.net GridView控件中實(shí)現(xiàn)全選的解決方案
在GridView中我們經(jīng)常要利用復(fù)選按鈕實(shí)現(xiàn)全選的功能,下面針對(duì)這一解決方案做以總結(jié)2010-03-03.net 中按.(點(diǎn))無(wú)法智能提示的bug解決方案
IDE按.無(wú)法智能提示,但是可以編譯并正常使用,在修改別人代碼bug時(shí)遇到的,接下來(lái)為你提供詳細(xì)解決方法,感興趣的你可以參考下哈2013-03-03GridView高效分頁(yè)和搜索功能的實(shí)現(xiàn)代碼
GridView高效分頁(yè)和搜索功能的實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-03-03asp.net+Ligerui實(shí)現(xiàn)grid導(dǎo)出Excel和Word的方法
這篇文章主要介紹了asp.net+Ligerui實(shí)現(xiàn)grid導(dǎo)出Excel和Word的方法,實(shí)例分析了asp.net結(jié)合jQuery的Ligerui插件操作excel和word文件的技巧,需要的朋友可以參考下2016-04-04