标签:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
namespace WebApplication2
{
/// <summary>
/// Handler 的摘要说明
/// </summary>
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string url = context.Request["med"];
HttpPostedFile postedFile = context.Request.Files[0];
string savePath = "/images/";
int filelength = postedFile.ContentLength;
string filename = postedFile.FileName;
int fileSize = 163600; //150K
string fileName = ""; //返回的上传后的文件名
if (filelength <= fileSize)
{
byte[] buffer = new byte[filelength];
postedFile.InputStream.Read(buffer, 0, filelength);
fileName = UploadImage(buffer, savePath, "jpg", url);
//fileName = UploadImage(buffer, savePath, filename, url);
}
else
{
fileName = "-1";
}
//fileName = fileName +","+ url;
context.Response.Write(fileName);
}
public static string UploadImage(byte[] imgBuffer, string uploadpath, string ext, string url)
{
try
{
System.IO.MemoryStream m = new MemoryStream(imgBuffer);
if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
if (File.Exists(url))
{
//如果存在则删除
File.Delete(url);
}
string imgname = CreateIDCode() + "." + ext;
string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname;
Image img = Image.FromStream(m);
img.Save(_path);
if (ext == "jpg")
img.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
else
img.Save(_path, System.Drawing.Imaging.ImageFormat.Gif);
m.Close();
return uploadpath + imgname;
// return _path;
}
catch (Exception ex)
{
return ex.Message;
}
}
public static string CreateIDCode()
{
DateTime Time1 = DateTime.Now.ToUniversalTime();
DateTime Time2 = Convert.ToDateTime("1970-01-01");
TimeSpan span = Time1 - Time2; //span就是两个日期之间的差额
string t = span.TotalMilliseconds.ToString("0");
return t;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
3,在线切割生成缩略图页面ImageSelectCutService.aspx
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class ImageSelectCutService : System.Web.UI.Page
{
#region 参数定义
/// <summary>
/// 两个坐标点的x和y坐标值
/// </summary>
public int x1, y1, x2, y2;
public string picPath = "";
public string newPicPath = "/images/new/new.jpg";
#endregion
protected void Page_Load(object sender, EventArgs e)
{
#region 获取参数
picPath = Request.QueryString["filename"];
string[] temp = picPath.Split(‘/‘);
newPicPath = "/images/new/" + temp[2];
x1 = string.IsNullOrEmpty(Request["x1"]) ? 0 : Convert.ToInt32(Request["x1"]);
y1 = string.IsNullOrEmpty(Request["y1"]) ? 0 : Convert.ToInt32(Request["y1"]);
x2 = string.IsNullOrEmpty(Request["x2"]) ? 0 : Convert.ToInt32(Request["x2"]);
y2 = string.IsNullOrEmpty(Request["y2"]) ? 0 : Convert.ToInt32(Request["y2"]);
///裁剪图片
CutImage(picPath, newPicPath, x1, y1, x2, y2);
#endregion
}
private void CutImage(string picPath, string newPicPath, int x1, int y1, int x2, int y2)
{
string returnJson = "";
//获取所选区域的高宽值
int width = x2 - x1;
int height = y2 - y1;
try
{
///将图片文件字节化
Bitmap bm = new Bitmap(Server.MapPath(picPath));
//处理JPG质量的函数
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
{
ici = codec;
break;
}
}
int level = 100; //图片质量
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);
// 裁剪图片
Rectangle cloneRect = new Rectangle(x1, y1, width, height);
PixelFormat format = bm.PixelFormat;
Bitmap cloneBitmap = bm.Clone(cloneRect, format);
//保存图片
cloneBitmap.Save(Server.MapPath(newPicPath), ici, ep);
cloneBitmap.Dispose();
bm.Dispose();
returnJson = "{\"state\":\"0\",\"msg\":\"" + newPicPath + "\"}";
}
catch (Exception e)
{
returnJson = "{\"state\":\"1\",\"msg\":\"" + e.Message + "\"}";
}
Response.Write(returnJson);
Response.End();
}
}
}
4,引用CSS样式imgareaselect-default.css
/*
* imgAreaSelect default style
*/
.imgareaselect-border1 {
background: url(border-v.gif) repeat-y left top;
}
.imgareaselect-border2 {
background: url(border-h.gif) repeat-x left top;
}
.imgareaselect-border3 {
background: url(border-v.gif) repeat-y right top;
}
.imgareaselect-border4 {
background: url(border-h.gif) repeat-x left bottom;
}
.imgareaselect-border1, .imgareaselect-border2,
.imgareaselect-border3, .imgareaselect-border4 {
filter: alpha(opacity=50);
opacity: 0.5;
}
.imgareaselect-handle {
background-color: #fff;
border: solid 1px #000;
filter: alpha(opacity=50);
opacity: 0.5;
}
.imgareaselect-outer {
background-color: #000;
filter: alpha(opacity=50);
opacity: 0.5;
}
.imgareaselect-selection {
}
5,引用JS
图片显示根据个人需要,有什么问题可以留言
标签:
原文地址:http://www.cnblogs.com/hqhouse/p/4220478.html