验证中...
C# MVC ActionResult方法
Raw Copy
///
///GetSiteDomainName 为后台网络地址
///
public ActionResult Img(string t)
{
if (!string.IsNullOrEmpty(Request.Params["t"]))
{
string urlParams = Request.Params["t"];
string[] urlParamsArray = urlParams.Split('_');
if (urlParamsArray.Length < 4)
{
return Content("非法访问");
}
//图片宽度
int imgWidth = int.Parse(urlParamsArray[0]);
//图片高度
int imgHeight = int.Parse(urlParamsArray[1]);
//图片缩放形式
int imgtype = int.Parse(urlParamsArray[2]);
//图片输出格式
int imgFormat = int.Parse(urlParamsArray[3]);
//图片名称
string imgName = urlParamsArray[4];
//图片保存路径
string fileSaveUrlPath = string.Empty;
//显示图片地址
string imgAdress = string.Empty;
if (!string.IsNullOrEmpty(imgName))
{
//保存文件名
string fileName = string.Format("{0}{1}{2}{3}_{4}",
imgWidth.ToString(), imgHeight.ToString(), imgtype.ToString(), imgFormat.ToString(), imgName);
string imgNameNotFormat = fileName.Substring(0, fileName.LastIndexOf("."));
//图片保存路径
fileSaveUrlPath = string.Format("~/Imgs/{0}/{1}", imgName.Substring(0, 4), Int32.Parse(imgName.Substring(4, 2)));
fileSaveUrlPath = Server.MapPath(fileSaveUrlPath);
StringUtils.CreateDirectory(fileSaveUrlPath);
fileSaveUrlPath = string.Format("{0}\\{1}.jpg", fileSaveUrlPath, imgNameNotFormat);
imgAdress = string.Format("/Imgs/{0}/{1}/{2}.jpg", imgName.Substring(0, 4), Int32.Parse(imgName.Substring(4, 2)), imgNameNotFormat);
}
if (StringUtils.FileExists(fileSaveUrlPath))
{
return Redirect(string.Format("{0}{1}", GetSiteDomainName, imgAdress));
}
else
{
imgName = string.IsNullOrEmpty(imgName) ? "0000000000000000" : imgName;
//图片路径
string fileUrlPath = string.Format("~/Images/{0}/{1}/{2}", imgName.Substring(0, 4), Int32.Parse(imgName.Substring(4, 2)), imgName);
fileUrlPath = Server.MapPath(fileUrlPath);
if (!StringUtils.FileExists(fileUrlPath))
{
//默认图片名称
string defaultName = string.Format("{0}{1}{2}{3}", imgWidth.ToString(),
imgHeight.ToString(), imgtype.ToString(), imgFormat.ToString());
fileUrlPath = Server.MapPath("~/Imgs/Default/Default.jpg");
fileSaveUrlPath = Server.MapPath(string.Format("~/Imgs/Default/{0}.jpg", defaultName));
imgAdress = string.Format("/Imgs/Default/{0}.jpg", defaultName);
}
if (!StringUtils.FileExists(fileSaveUrlPath))
{
GDIUtils.GetImage(fileUrlPath, fileSaveUrlPath, imgWidth, imgHeight, imgtype, imgFormat);
}
return Redirect(string.Format("{0}{1}", GetSiteDomainName, imgAdress));
}
}
return null;
}
图片处理类
Raw Copy
public class GDIUtils
{
/// <summary>
/// 获取图片param
/// </summary>
/// <param name="fileUrlPath">图片路径</param>
/// <param name="newfileUrlPath">图片保存路径</param>
/// <param name="PWidth">宽度</>
/// <param name="PHeight">高度</param>
/// <param name="Ptype">图片输入形式t=1,按照输入宽高形成png背景图片;t=2,按照图片宽高等比(输入宽高约束)形成等比缩放</param>
/// <param name="PFormat">图片输出格式1:jpg,2:png,3:gif,4:bmp</param>
/// <returns></returns>
public static void GetImage(string fileUrlPath, string newfileUrlPath, int PWidth, int PHeight, int Ptype, int PFormat)
{
//获取图片绝对路径
string UserDirectory = fileUrlPath;
System.IO.MemoryStream MTms = OnlineMakeThumbnail(UserDirectory, PWidth, PHeight, Ptype);
System.Drawing.Image MTimg = System.Drawing.Image.FromStream(MTms);
if (PFormat == 1)
MTimg.Save(newfileUrlPath, System.Drawing.Imaging.ImageFormat.Jpeg);
else if (PFormat == 2)
MTimg.Save(newfileUrlPath, System.Drawing.Imaging.ImageFormat.Png);
else if (PFormat == 3)
MTimg.Save(newfileUrlPath, System.Drawing.Imaging.ImageFormat.Gif);
else
MTimg.Save(newfileUrlPath, System.Drawing.Imaging.ImageFormat.Bmp);
MTimg.Dispose();
MTms.Close();
}
/// <summary>
/// 功能:在线动态加载指定大小图片
/// </summary>
/// <param name="rPath"></param>
/// <param name="PWidth">图片宽度</param>
/// <param name="PHeight">图片高度</param>
/// <param name="typeID">t=1,按照输入宽高形成png背景图片;t=2,按照图片宽高等比(输入宽高约束)形成等比缩放</param>
/// <returns></returns>
public static System.IO.MemoryStream OnlineMakeThumbnail(string rPath, float PWidth, float PHeight, int typeID)
{
System.Drawing.Image oImage = null;
System.Drawing.Image bitmap = null;
try
{
oImage = System.Drawing.Image.FromFile(rPath);
float rw = 0; //最后计算所得宽度
float rh = 0; //最后计算所得高度
float fw = PWidth; //范围值宽度
float fh = PHeight; //范围值高度
float fb = fw / fh;
float yw = oImage.Width; //原图宽度
float yh = oImage.Height; //原图高度
float yb = yw / yh;
if (fb >= yb)
{
rw = fh * yb;
rh = fh;
}
else
{
rw = fw;
rh = fw / yb;
}
//新建一个bmp图片
if (typeID == 1)
{
bitmap = new System.Drawing.Bitmap((int)PWidth, (int)PHeight);
}
else
{
bitmap = new System.Drawing.Bitmap((int)rw, (int)rh);
}
return ToPicture(oImage, bitmap, PWidth, PHeight, rw, rh, typeID);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 功能:建立新的图片
/// </summary>
/// <param name="oImage"></param>
/// <param name="bitmap"></param>
/// <param name="PWidth"></param>
/// <param name="PHeight"></param>
/// <param name="rw"></param>
/// <param name="rh"></param>
/// <param name="typeID"></param>
/// <returns></returns>
private static System.IO.MemoryStream ToPicture(System.Drawing.Image oImage, System.Drawing.Image bitmap, float PWidth, float PHeight, float rw, float rh, int typeID)
{
System.Drawing.Graphics g = null;
System.IO.MemoryStream ms = null;
try
{
//新建一个画板
g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//图片定位坐标
int px = 0;
int py = 0;
if (typeID == 1)
{
px = (int)((PWidth - rw) / 2);
py = (int)((PHeight - rh) / 2);
}
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(oImage, new Rectangle(px, py, (int)rw, (int)rh),
new System.Drawing.Rectangle(0, 0, oImage.Width, oImage.Height),
System.Drawing.GraphicsUnit.Pixel);
ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);// 格式自处理,这里用 bitmap
return ms;
}
catch (Exception ex)
{
throw ex;
}
finally
{
oImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
前台Html调用方法
Raw Copy
<img src="@WebSite.Common.ImageUtils.GetImageUrl(图片宽度, 图片高度, "图片路径")" alt="">
public class ImageUtils
{
public static string GetImageUrl(Int32 width, Int32 height, string nameurl)
{
if (!string.IsNullOrEmpty(nameurl))
{
nameurl = nameurl.Split('/').LastOrDefault();
}
return string.Format("{0}?t={1}_{2}_1_2_{3}", ConfigUtils.GetConfigString("ImageUrl"), width.ToString(), height.ToString(), nameurl);
}
}
C# 保存图片类
Raw Copy
[HttpPost]
public ActionResult ImgSave(HttpPostedFileBase file)
{
if (file == null)
return null;
//获取文件格式
string _fileExt = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
//文件保存路径
string fileUrlPath = string.Format("/Images/{0}/{1}/", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString());
string Path = Server.MapPath(string.Format("~{0}", fileUrlPath));
string FileDirectory = CreateDirectory(Path);
string _fileName = string.Format("{0}.{1}", getTimeStr, _fileExt); //随机文件名
//保存文件
file.SaveAs(FileDirectory + _fileName);
return Content(fileUrlPath + _fileName);
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_you_jiang_zheng_wen Zheng_wen_close