1、水印
1.1简单水印:
<form method="post" action="Upload.ashx" enctype="multipart/form-data">
<input type="file" name="f"/>
<br />
<input type="submit" value="上传" />
</form>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"
HttpPostedFile f = context.Request.Files["f"]
//根据文件输入流,创建画布。
Bitmap bitmap = new Bitmap(f.InputStream)
//获取绘制工具
Graphics g = Graphics.FromImage(bitmap)
//读取水印文件
Bitmap water = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "Uploads/logo.jpg")
//绘制
g.DrawImage(water, bitmap.Width - water.Width, bitmap.Height - water.Height, water.Width, water.Height)
//保存
bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "Uploads/" + f.FileName)
}
1.2使用封装的Imageclass
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"
HttpPostedFile img = context.Request.Files["img"]
string waterImgSrc = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\logo.jpg"
string savePath = AppDomain.CurrentDomain.BaseDirectory + "Uploads/big/"
img.SaveAs(savePath + img.FileName)
ImageClass.ImageWatermark(savePath + img.FileName, waterImgSrc, "")
context.Response.Write("ok")
}
2、缩略图
2.1简单缩略图:
<form method="post" action="Thumbnail.ashx" enctype="multipart/form-data">
<input type="file" name="img" />
<br />
<input type="submit" value="保存" />
</form>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"
HttpPostedFile img = context.Request.Files["img"]
string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads"
img.SaveAs(path + "/big/" + img.FileName)
Bitmap big = new Bitmap(img.InputStream)
Bitmap small = new Bitmap((int)(big.Width * 0.1), (int)(big.Height * 0.1))
Graphics g = Graphics.FromImage(small)
g.DrawImage(big, 0, 0, small.Width, small.Height)
small.Save(path + "/small/" + img.FileName,ImageFormat.Jpeg)
context.Response.Write("ok")
}
2.2使用封装好的Imageclass
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"
HttpPostedFile img = context.Request.Files["img"]
string pathBigimg = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\big\\"
string pathSmallimg = AppDomain.CurrentDomain.BaseDirectory + "Uploads\\small\\"
img.SaveAs(pathBigimg + img.FileName)
ImageClass.MakeThumbnail(pathBigimg + img.FileName, pathSmallimg+img.FileName, 300, 200, "Cut")
context.Response.Write("原图:<br /><img src='../Uploads/big/"+img.FileName+ "'/><br />缩略图:<br /><img src='../Uploads/small/" +img.FileName+"'>")
}
附:ImageClass