C#图片压缩的实现方法
一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的。尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如果是需要高质量的图片也得需要生产缩略图。
下面贴出我自己琢磨的图片压缩算法,首先这个是未经优化的简单实现:
public static system.drawing.image getimagethumb(system.drawing.image sourceimg, int width, int height)
{
system.drawing.image targetimg = new system.drawing.bitmap(width, height);
using (system.drawing.graphics g = system.drawing.graphics.fromimage(targetimg))
{
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
g.compositingquality = system.drawing.drawing2d.compositingquality.highquality;
g.pixeloffsetmode = system.drawing.drawing2d.pixeloffsetmode.highquality;
g.drawimage(sourceimg, new system.drawing.rectangle(0, 0, width, height), new system.drawing.rectangle(0, 0, sourceimg.width, sourceimg.height), system.drawing.graphicsunit.pixel);
g.dispose();
}
return targetimg;
}
这个方法比较简单,用到的是高质量压缩。经过这个方法压缩后,200k的图片只能压缩到160k左右。经过改写代码实现了如下的方法:
public bitmap getimagethumb(bitmap mg, size newsize)
{
double ratio = 0d;
double mythumbwidth = 0d;
double mythumbheight = 0d;
int x = 0;
int y = 0;
bitmap bp;
if ((mg.width / convert.todouble(newsize.width)) > (mg.height /
convert.todouble(newsize.height)))
ratio = convert.todouble(mg.width) / convert.todouble(newsize.width);
else
ratio = convert.todouble(mg.height) / convert.todouble(newsize.height);
mythumbheight = math.ceiling(mg.height / ratio);
mythumbwidth = math.ceiling(mg.width / ratio);
size thumbsize = new size((int)newsize.width, (int)newsize.height);
bp = new bitmap(newsize.width, newsize.height);
x = (newsize.width - thumbsize.width) / 2;
y = (newsize.height - thumbsize.height);
system.drawing.graphics g = graphics.fromimage(bp);
g.smoothingmode = smoothingmode.highquality;
g.interpolationmode = interpolationmode.highqualitybicubic;
g.pixeloffsetmode = pixeloffsetmode.highquality;
rectangle rect = new rectangle(x, y, thumbsize.width, thumbsize.height);
g.drawimage(mg, rect, 0, 0, mg.width, mg.height, graphicsunit.pixel);
return bp;
}
这样实现的压缩使压缩率大幅度上升。其实代码并没有变多少,最主要的是在保存的时候要是用jpg格式,如果不指定格式,默认使用的是png格式。
下面这个是园友写的根据设置图片质量数值来压缩图片的方法:
public static bool getpicthumbnail(string sfile, string outpath, int flag)
{
system.drawing.image isource = system.drawing.image.fromfile(sfile);
imageformat tformat = isource.rawformat;
//以下代码为保存图片时,设置压缩质量
encoderparameters ep = new encoderparameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
encoderparameter eparam = new encoderparameter(system.drawing.imaging.encoder.quality, qy);
ep.param[0] = eparam;
try
{
imagecodecinfo[] arrayici = imagecodecinfo.getimageencoders();
imagecodecinfo jpegiciinfo = null;
for (int x = 0; x < arrayici.length; x++)
{
if (arrayici[x].formatdescription.equals("jpeg"))
{
jpegiciinfo = arrayici[x];
break;
}
}
if (jpegiciinfo != null)
{
isource.save(outpath, jpegiciinfo, ep);//dfile是压缩后的新路径
}
else
{
isource.save(outpath, tformat);
}
return true;
}
catch
{
return false;
}
finally
{
isource.dispose();
isource.dispose();
}
}
转载来源:http://www.cnblogs.com/lifeil/archive/2013/02/25/2931683.html