截图上传功能 imageAreaselect
阅读原文时间:2023年07月13日阅读:1

前台:




图片上传:\*.BMP \*.PNG \*.JPG \*.JPEG

@\* @using (Html.BeginForm("imgEditHandler", "EditUploadImg", FormMethod.Post, new { Area = "TestFunction" })) {
\*@
@\*}\*@
@\*

\*@
@if (ViewData\["success"\] != null) { } else { 上传失败 }



后台:

public ActionResult SaveImg(HttpPostedFileBase uploadImageFile)
{
//保存上传图片
// 随机数
string img = uploadImageFile.FileName;
int index = img.IndexOf('.');
string[] fileName = img.Split('.');
var rand = new Random().Next(0, 8999);
// 创建文件存放的文件路径,如果没有,创建该路径
string tempImg = "~/Content/UploadImg/";//路径模板
string folder = HttpContext.Server.MapPath(tempImg);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
// 保存全文件路径
string filePath = Path.Combine(folder, fileName[0] + rand + "." + fileName[1]);

//写cookie
//HttpContext.Response.Cookies["srcImg"].Value = filePath;
HttpCookie cookies = new HttpCookie("srcImg");
cookies.Value = HttpUtility.UrlEncode(filePath, Encoding.GetEncoding("UTF-8")); ;
cookies.Expires = DateTime.Now.AddHours(1);
HttpContext.Response.Cookies.Add(cookies);
uploadImageFile.SaveAs(filePath);
//return Json(new { success=true,pathsrc=filePath});
return Content(filePath);
//保存成功
}
///

/// 截取 ///
///
///
public ActionResult imgEditHandler(int xx1,int xx2,int yy1,int yy2)
{
string tempImg = "~/Content/UploadImg/";//路径模板
int x1 = Convert.ToInt32(xx1);
int y1 = Convert.ToInt32(yy1);
int x2 = Convert.ToInt32(xx2);//实际上是没有用的
int y2 = Convert.ToInt32(yy2);//实际上是没有用得
//string srcImg = tempImg + fileName[0] + rand + "." + fileName[1];//原图图片存在路径
//string srcImg = form["src"];
//到cookie 拿路径
//string srcImg = Common.DEncrypt.DEncrypt.Decrypt(HttpContext.Request.Cookies["srcImg"].Value);
string srcImg = HttpUtility.UrlDecode(Common.CommonClass.GetCookie("srcImg"), Encoding.GetEncoding("UTF-8")); ; ;
//string srcPath = HttpContext.Server.MapPath(srcImg);//实际路径
//创建图象,保存将来截取的图象
//Bitmap image = new Bitmap(Path);
//Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域
//imgGraphics.CopyFromScreen(x1, y1, x2, y2, new Size(100, 100));
//保存
//SaveImage(image);
string[] fileName = srcImg.Split('.');
//string file = fileName[0]+fileName[1] + "caijinhao." + fileName[2];
string folders = HttpContext.Server.MapPath("~/Content/UploadImg/");//映射路径
string newpath = CutImage(srcImg, x1, y1, x2, y2, Guid.NewGuid().ToString("N"), folders, fileName[fileName.Length - 1]);
ViewData["success"] = "true";
return View("img3");
}
/// /// 截取图片方法 ///
/// 图片地址
/// 开始位置-X
/// 开始位置-Y
/// 截取宽度
/// 截取长度
/// 文件名称
/// 保存路径
/// 后缀名
public string CutImage(string url, int beginX, int beginY, int getX, int getY, string fileName, string savePath, string fileExt)
{
//if ((beginX < getX) && (beginY < getY))
//{
Bitmap bitmap = new Bitmap(url);//原图
//if (((beginX + getX) <= bitmap.Width) && ((beginY + getY) <= bitmap.Height))
//{
// Bitmap destBitmap = new Bitmap(getX, getY);//目标图 创建新图大小
Bitmap destBitmap = new Bitmap(100, 100);//要存储的图像的大小
Rectangle destRect = new Rectangle(0, 0, getX, getY);//我要在新图的开始画图的位置
Rectangle srcRect = new Rectangle(beginX, beginY, getX, getY);//我要在原图的这个位置复制
Graphics imgGraphics = Graphics.FromImage(destBitmap);
imgGraphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
ImageFormat format = ImageFormat.Png;
switch (fileExt.ToLower())
{
case "png": format = ImageFormat.Png; break;
case "bmp": format = ImageFormat.Bmp; break;
case "gif": format = ImageFormat.Gif; break;
}
destBitmap.Save(savePath + "//" + fileName + "." + fileExt, format);
return savePath + "\\" + "*" + fileName.Split('.')[0] + "." + fileExt;
//}
//else
//{ return "截取范围超出图片范围"; }
//}
//else
//{ return "请确认(beginX < getX)&&(beginY < getY)"; }
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器