上传图片和等比例压缩

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Web;

using System.Collections;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI;

using System.IO;

 

namespace ShareLibrary

{

public class Picture

{

public string[] SavePicture(string url,FileUpload fiu)

{

string fillname, type, Fillname;

string[] Path = new string[2];

Page p = new Page();

if (!string.IsNullOrEmpty(fiu.PostedFile.FileName))

{

fillname = fiu.PostedFile.FileName.ToString();

type = fiu.PostedFile.ContentType;

if (type == "image/jpeg" || type == "image/png" || type == "image/gif" || type == "image/pjpeg" || type == "image/x-png")

{

int i = fillname.LastIndexOf(".");

int j = fillname.Length;

string suffy = fillname.Substring(i, j - i);//获取图片后缀名

Fillname = Guid.NewGuid().ToString();

Path[1] = p.Server.MapPath(url) + Fillname.Trim() + suffy;

Path[0] = url + Fillname + suffy;

fiu.SaveAs(Path[1]);

}

return Path;

}

else

{

Path[0] = "";

Path[1] = "";

return Path;

}

}

/// 缩小图片

/// </summary>

/// <param name="strOldPic">源图文件名(包括路径)</param>

/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>

/// <param name="intWidth">缩小至宽度</param>

/// <param name="intHeight">缩小至高度</param>

public string SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)

{

System.Drawing.Bitmap objPic, objNewPic;

Page p = new Page();

string _objNewPic = "";

try

{

if (strOldPic == "default.jpg")

{

objPic = new System.Drawing.Bitmap(p.Server.MapPath(strNewPic + strOldPic));

objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);

_objNewPic = strNewPic + Guid.NewGuid().ToString() + ".jpg";

objNewPic.Save(p.Server.MapPath(_objNewPic));

}

else

{

objPic = new System.Drawing.Bitmap(p.Server.MapPath(strOldPic));

objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);

string suffer = strOldPic.Substring(strOldPic.LastIndexOf("."), strOldPic.Length - strOldPic.LastIndexOf("."));

_objNewPic = strNewPic + Guid.NewGuid().ToString() + suffer;

objNewPic.Save(p.Server.MapPath(_objNewPic));

}

}

catch (Exception exp)

{ _objNewPic = strOldPic; }

finally

{

objPic = null;

objNewPic = null;

}

return _objNewPic;

}

 

}

}

  

posted @ 2016-04-18 15:02  装饰的梦  阅读(264)  评论(0)    收藏  举报