首页 | 博客首页| 帮助 | 退出
雨天的感觉的博客
http://fanghonglei.cityphotos.cn
标题 标签
注册名:
     博文 | 评论 | 留言 | 好友管理 |
我的博文
 
· C#高质量缩略图 (2009-03-11 10:42)
  标签:图片  缩小  
 

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)
    
{
        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( 00, newSize.Width, newSize.Height ),
    
00, 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的质量参数。
代码:

引用
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://images.shouker.com/imgc1/114/114232.jpg";
}
}

 


 
 
 
网站导航 | 广告业务 | 关于我们 | 帮助 | 浙ICP备05017988号
地址:中国浙江省嘉兴市迎宾大道99号 邮编:314050 Email(msn):cityphotos@live.cn 售图电话:0573-82532688
嘉兴日报社版权所有 未经授权禁止复制或建立镜像