所谓c#图片处理高级应,多数是基于.net framework类库完成
namespace wujian.common
{
/// <summary>
/// 图片处理类
/// </summary>
public class ptimage
{
#region 正方型裁剪并缩放
/// <summary>
/// 正方型裁剪
/// 以图片中心为轴心,截取正方型,然后等比缩放
/// 用于头像处理
/// </summary>
/// <param name="postedfile">原图httppostedfile对象</param>
/// <param name="filesaveurl">缩略图存放地址</param>
/// <param name="side">指定的边长(正方型)</param>
/// <param name="quality">质量(范围0-100)</param>
public static void cutforsquare(system.web.httppostedfile postedfile, string filesaveurl, int side, int quality)
{
//创建目录
string dir = path.getdirectoryname(filesaveurl);
if (!directory.exists(dir))
directory.createdirectory(dir);
//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
system.drawing.image initimage = system.drawing.image.fromstream(postedfile.inputstream, true);
//原图宽高均小于模版,不作处理,直接保存
if (initimage.width <= side && initimage.height <= side)
{
initimage.save(filesaveurl, system.drawing.imaging.imageformat.jpeg);
}
else
{
//原始图片的宽、高
int initwidth = initimage.width;
int initheight = initimage.height;
//非正方型先裁剪为正方型
if (initwidth != initheight)
{
//截图对象
system.drawing.image pickedimage = null;
system.drawing.graphics pickedg = null;
//宽大于高的横图
if (initwidth > initheight)
{
//对象实例化
pickedimage = new system.drawing.bitmap(initheight, initheight);
pickedg = system.drawing.graphics.fromimage(pickedimage);
//设置质量
pickedg.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic;
pickedg.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//定位
rectangle fromr = new rectangle((initwidth - initheight) / 2, 0, initheight, initheight);
rectangle tor = new rectangle(0, 0, initheight, initheight);
//画图
pickedg.drawimage(initimage, tor, fromr, system.drawing.graphicsunit.pixel);
//重置宽
initwidth = initheight;
}
//高大于宽的竖图
else
{
//对象实例化