@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div>
<form enctype="multipart/form-data" action="/show/Index" method="post">
<input id="File1" type="file" name="ExcFile" /><br />
<br />
输入水印文字:<br />
<br />
<input id="Text1" type="text" name="Shuiyinwenzi" /><br />
<br />
<input id="Button1" type="submit" value="添加水印" onclick="shui()" />
<br />
<input id="Button1" type="button" value="导出图片" onclick="xiazai()" />
</form>
</div>
加载图片
<img src="@ViewBag.xia" width="150" height="150" id="img1" />
</body>
</html>
<script>
function xiazai()
{
location.href = '/show/ss';
}
</script>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using BLL;
using System.Drawing;
namespace yanglishuang.Controllers
{
public class showController : Controller
{
// GET: show
stuBLL bll = new stuBLL();
public ActionResult Index( )
{
// ViewBag.xia = @"EXCEL1.jpg";
return View();
}
/// <summary>
/// tinajia
/// </summary>
/// <param name="Exc"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Index(string Shuiyinwenzi, HttpPostedFileBase ExcFile)
{
if (ExcFile == null)
{
return View();
}
string strename = Server.MapPath("/EXCEL/");
if (!Directory.Exists(strename))
{
Directory.CreateDirectory(strename);
}
string filename = Path.GetFileName(ExcFile.FileName);
ExcFile.SaveAs(strename + filename);
Image image = Image.FromFile(strename + filename); //路径
Graphics gra = Graphics.FromImage(image);//创建
String text = Shuiyinwenzi;
Font font = new Font("宋体", 45);//字体大小
SizeF size = new SizeF();//宽度
SolidBrush seli = new SolidBrush(Color.FromArgb(255, 255, 0, 0));//颜色
gra.DrawString(text, font, seli, new PointF(image.Width / 2, image.Height / 2 - size.Height / 2));
//路径
image.Save(@"C:Users过客Desktop练习水yanglishuangyanglishuangEXCEL feb8023b3a29c00bd929e2ac5fb28aa.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//页面显示
Session["name"] = @"C:Users过客Desktop练习水yanglishuangyanglishuangEXCEL feb8023b3a29c00bd929e2ac5fb28aa.jpg";
//添加
stuModel m = new stuModel()
{
Name = Shuiyinwenzi,//水印文本
wuli = Session["name"].ToString(),//物理路径
geshi = filename//后缀名格式
};
bll.add(m);//添加到三层
return Content("<script>alert('水印成功!');location.href='/show/Index'</script>");//成功返回视图
}
/// <summary>
/// 简单下载
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public ActionResult ss(string name= "0feb8023b3a29c00bd929e2ac5fb28aa.jpg")
{
// var newname = name;//路径
//string path = Server.MapPath("/EXCEL/");//文件夹
// var allpath = path + newname;
var allpath = Server.MapPath("/EXCEL/") + "0feb8023b3a29c00bd929e2ac5fb28aa.jpg";
Image image = Image.FromFile(allpath);//创建一个
var xiazai = @"E:img" + Guid.NewGuid().ToString() + ".png";//新的突破名称 路径
//保存
image.Save(xiazai);
//成功
return Content("<script>alert('导出成功!');location.href='/show/Index'</script>");
}
}
}