using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Labthink.BI.Models;
namespace Labthink.BI.Helpers
{
public class UploadHelper
{
private DbEntities context = null;
/// <summary>
/// 构造函数
/// </summary>
public UploadHelper(DbEntities context)
{
this.context = context;
}
#region 上传并裁切图片
/// <summary>
/// 设置裁切上传图片
/// </summary>
/// <param name="uploadFile">已上传的文件对象</param>
/// <param name="width">选区的宽度</param>
/// <param name="height">选区的高度</param>
/// <param name="left">选区的左偏移量</param>
/// <param name="top">选区的上偏移量</param>
/// <param name="url">图片要保存的URL</param>
/// <param name="msg">返回消息</param>
/// <returns></returns>
public bool SetPicture(HttpPostedFileBase uploadFile, decimal width, decimal height, decimal left, decimal top, string url, out string msg)
{
string uploadFileName = Path.GetFileName(uploadFile.FileName);
string uploadExtension = Path.GetExtension(uploadFile.FileName).ToLower(); // 文件扩展名
string uploadFileType = uploadFile.ContentType.ToLower(); // 文件类型
// 判断文件扩展名
if (!".png".Equals(uploadExtension) && !".jpg".Equals(uploadExtension) && !".jpeg".Equals(uploadExtension) && !".gif".Equals(uploadExtension))
{
msg = "上传文件格式不符(必须是png、jpg、gif格式),上传被阻止!";
return false;
}
// 判断文件类型
if (!"image/png".Equals(uploadFileType) && !"image/x-png".Equals(uploadFileType) && !"image/pjpeg".Equals(uploadFileType) && !"image/jpeg".Equals(uploadFileType) && !"image/gif".Equals(uploadFileType))
{
msg = "上传文件格式不符(必须是png、jpg、gif格式),上传被阻止!";
return false;
}
// 判断图片文件大小
if (uploadFile.ContentLength > 1024 * 1024 * 2)
{
msg = "文件大小超出 2M 的限制,上传被阻止!";
return false;
}
/********************************************
* 以下按照页面选择的区域对图片进行裁切并压缩
**/
Image uploadImage = null;
Image corpImage = null;
Graphics corpGraphics = null;
// 获取图片保存路径
FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath(url));
if (!file.Exists)
{
file.Directory.Create();
}
try
{
uploadImage = Image.FromStream(uploadFile.InputStream); // 加载上传的图片到 Image 对象
// 定义要裁切的矩形区域
decimal cutX = left * (uploadImage.Width / width);
decimal cutY = top * (uploadImage.Height / height);
decimal cutWidth = 100 * (uploadImage.Width / width);
decimal cutHeight = 100 * (uploadImage.Height / height);
Rectangle corpArea = new Rectangle(Convert.ToInt32(Math.Round(cutX)), Convert.ToInt32(Math.Round(cutY)), Convert.ToInt32(Math.Round(cutWidth)), Convert.ToInt32(Math.Round(cutHeight)));
// 定义要生成的图片对象
corpImage = new Bitmap(100, 100);
corpGraphics = Graphics.FromImage(corpImage);
corpGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; // 设置高质量插值法
corpGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 设置高质量,低速度呈现平滑程度
corpGraphics.Clear(System.Drawing.Color.Transparent); // 清空画布并以透明背景色填充
// 在指定位置并且按指定大小绘制原图片的指定部分
corpGraphics.DrawImage(uploadImage, new System.Drawing.Rectangle(0, 0, 100, 100), corpArea, GraphicsUnit.Pixel);
// 保存裁切的图片
corpImage.Save(file.FullName, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception ex)
{
// ex.Handle("上传裁切图片出错(fileName = " + file.FullName + ")");
msg = "上传裁切图片出错!";
return false;
}
finally
{
// 销毁对象,释放内存
uploadImage.Dispose();
corpImage.Dispose();
corpGraphics.Dispose();
}
msg = "图片上传裁切成功!";
return true;
}
#endregion
}
}