一: 图片加水印处理
//获得图片的名称
string name = context.Request["name"];
string filePath = context.Server.MapPath("Upload/" + name);
//首先获得要进行水印处理的图片
if (!string.IsNullOrEmpty(name))
{
using (Image image = Bitmap.FromFile(filePath))
{
//准备一个画笔来在图片上作画
using (Graphics g = Graphics.FromImage(image))
{
//作画
g.DrawString("str", new Font("微软雅黑", 20), Brushes.Red, new PointF(1, 1));
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
二:生成缩略图
HttpPostedFile hpFile= context.Request.Files[0];
if (hpFile.ContentLength > 0)
{
string filePath = context.Server.MapPath("Upload/" + hpFile.FileName);
//判断文件是否为图片文件
if (hpFile.ContentType.IndexOf("image") > -1)
{
//从输入流中获得图片数据
using (Image img = Image.FromStream(hpFile.InputStream))
{
//准备一张画板来存储小图
using (Bitmap thumbImg = new Bitmap(120, 80))
{
//利用画笔将大图绘制在小图中
using (Graphics g = Graphics.FromImage(thumbImg))
{ //用于指定最终画的图的大小
g.DrawImage(img, new Rectangle(0, 0, thumbImg.Width, thumbImg.Height),
new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
//用于指定画大图中的那一部分
//将小图也保存在相应的路径中
thumbImg.Save(context.Server.MapPath("Upload/thumb_" + hpFile.FileName));
context.Response.Write("保存成功!");
}
}
}
}
//保存大图
hpFile.SaveAs(filePath);
}