照片列表里面的缩略照片都是有固定的,相同的宽度,和高度。但是呢,点开链接后,看到的大图都是有不同的宽和高,不同的比例。我知道小图的实际大小就是那么小。怎么做到将这些不同的大图,缩略成固定大小的小图,而又不失真!!!
解决方案 »
- 求取html中的所有图片地址正则表达式
- 高分求一个好使的邮件发送代码(VS03)
- asp.net关于到一定时间,执行的SQL操作
- 关于报表的问题 熟悉的朋友 出来解决一下 谢谢
- asp.net 动态生成控件问题
- 求助:存储过程执行时间稍长(1分钟),.net程序提示超时,如何处理?
- 用控件Table中的数据更新DataTable问题?
- 我的treeview是将整个树读到本地,不用SelectedDataIndex,可是为什么读树的时候那么慢??????????
- DataGrid中RadioButton取值问题!
- 初学ASP.NET,帮忙把一句C#转为VB的,谢谢
- asp.net 指令中如何使用表达式 在线等~~~~~~~~~~~
- 如何在gridview 的标头上加上一个checkbox
http://www.cnblogs.com/rock_chen/archive/2006/08/05/468387.html
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 ) );
}public static void SendSmallImage( string fileName,
string newFile,
int maxHeight,
int maxWidth )
{
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 );
Bitmap outBmp = new Bitmap( newSize.Width, newSize.Height );
Graphics g = Graphics.FromImage( outBmp );
// 设置画布的描绘质量
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( );
// 以下代码为保存图片时,设置压缩质量
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( );
}
Graphics 设置为 InterpolationMode.HighQualityBicubic 这样保证缩略算法最优化,生成最高质量的缩略图。
即使这样生成JPEG图片的文件,效果也不是很理想的。后来发现.net可以给编码器赋参数。也就是说可以设置JPEG的质量参数。效果对比:
原图设置InterpolationMode.HighQualityBicubic,没有设置JPGE质量参数设置InterpolationMode.HighQualityBicubic,设置JPGE质量参数为100
代码:
引用
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using System.Drawing.Drawing2D;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}/// <summary>
/// 得到文件代码的二进制码流
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private byte[] getFileCodebyURL(string url)
{
byte[] page;
try
{
WebClient client = new WebClient();
page = client.DownloadData(url);
}
catch
{
page = null;
}
return page;
}
protected void Button1_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream(getFileCodebyURL(TextBox1.Text));
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(ms);
int x, y, w, h;
if (originalImage.Width > 160 && originalImage.Height > 120)
{
w = 160;
h = 160 * originalImage.Height / originalImage.Width;
if (h > 120)
{
h = 120;
w = 120 * originalImage.Width / originalImage.Height;
x = (160 - w) / 2;
y = 0;
}
else
{
x = 0;
y = (120 - h) / 2;
}
}
else if (originalImage.Width > 160)
{
w = 160;
h = 160 * originalImage.Height / originalImage.Width;
x = 0;
y = (120 - h) / 2;
}
else if (originalImage.Height > 120)
{
h = 120;
w = 120 * originalImage.Width / originalImage.Height;
x = (160 - w) / 2;
y = 0;
}
else
{
w = originalImage.Width;
h = originalImage.Height;
x = (160 - w) / 2;
y = (120 - h) / 2;
}
Bitmap bm = new Bitmap(160, 120);
Graphics g = Graphics.FromImage(bm);// 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;// 指定高质量、低速度呈现。
//g.SmoothingMode = SmoothingMode.HighQuality;g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;g.Clear(Color.White);
g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);long[] quality = new long[1];
quality[0] = 100;System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo jpegICI = null;
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i];//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
bm.Save(Server.MapPath("upload/c.jpg"), jpegICI, encoderParams);
}bm.Dispose();
originalImage.Dispose();
g.Dispose();
Image1.ImageUrl = "http://localhost:7374/caipiao/upload/c.jpg";
}
}