首先创建一个文件,用于存放图片:Upload
UploadImg.htm:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
window.onload = function() {
var dom = document.getElementById("img");
dom.onchange = function() {
//alert(dom.value);
//如果是图片的话,那么放行,不是图片,那么剁掉了。
var index = dom.value.lastIndexOf(".");
//substr 从什么地方开始截取字符串。如果不设置最后一个参数。截取到最后
var ext = dom.value.substr(index + 1);
if (ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "png") {
} else {
dom.value = "";
}
};
};
</script>
</head>
<body>
<!--如果要上传文件必须使用multipart/form-data属性值-->
<form action="UploadImg.ashx" method="POST" enctype="multipart/form-data">
文件名:<input type="text" name="txtName" value="23"/><br/>
<input type="file" name="img" id="img"/><!--上传文件,则需要inputfile类型的控件-->
<br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
Upload.ashx.cs
public class UploadImg : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//首先能拿到当前传来的 文件
HttpPostedFile file = context.Request.Files["img"];
//file.FileName 拿到上传文件的文件名
if (file == null)
{
context.Response.Write("请选中文件");
return;
}
var ext = Path.GetExtension(file.FileName);//获取文件的扩展名
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png")
{
//把文件保存到后台服务器上
//SaveAs 接受一个绝对路径。
string filePath = "/Upload/" + Guid.NewGuid().ToString() + file.FileName;
string smallFilePath = "/Upload/Small-" + Path.GetFileName(filePath);
file.SaveAs(context.Request.MapPath(filePath));
context.Response.Write("<html><head></head><body><img src='" + filePath + "' /></body></html>");
//如何实现图片水印
//把文件保存到上传文件文件夹里面去
//接受的是一个绝对路径。
//file.SaveAs(context.Request.MapPath(fileName));
//图片保存到网站的目录之前,先加上水印。 InputStream:指向了当前上传文件的流。
Image image = Image.FromStream(file.InputStream);
Graphics g = Graphics.FromImage(image);//在图片上创建一个画布。
g.DrawString("红太阳",new Font("黑体",20),new SolidBrush(Color.Red),image.Width-60,image.Height-20 );//在画布上画 字符串。
image.Save(context.Request.MapPath(filePath ));//把图片保存到磁盘
//对图片生成缩率图。(方法1)
//把一张大图,画到一张小图上去,那么就小了。
//定义一张小图
Bitmap bitmap=new Bitmap(40,40);
//在小图上创建一个画布
Graphics gSmall = Graphics.FromImage(bitmap);
gSmall.DrawImage(image,0,0,40,40);
bitmap.Save(context.Request.MapPath(smallFilePath));
//对图片生成缩率图。(方法2)
使用的前提是在添加了一个类,用于生成缩略图ImageHelper.cs(见文末)
string orignPath = context.Request.MapPath(fileName);
file.SaveAs(orignPath);
Common.ImageHelper.MakeThumbnail(orignPath, context.Request.MapPath(smallFilePath),40,40,"W");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
ImgeHelper.cs\
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Heima8Company.Common
{
public class ImageHelper
{
///<summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形) 原始图和生成缩略图的宽高比相同那么就不变形
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
}