39,083
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<style>
*{margin:0;padding:0;}
</style>
</head>
<body>
<div style="display:inline-block;position:relative;">
<img src="img.jpg" border="0"><div id="view" style="position:absolute;left:0;top:0;display:inline-block;"><img src="2.jpg" border="0" ></div></div>
<form action="data.ashx" method="post" enctype="multipart/form-data" target="_blank">
<input type="hidden" name="t" id="t" />
<input type="hidden" name="l" id="l" />
<input type="submit" value="提交" id="btnSubmit" />
</form>
<script>
(function () {
var t = document.getElementById("t");
var l = document.getElementById("l");
var head = document.getElementById("view");
var img_width = 0;
var img_height = 0;
var position_x = 0;
var position_y = 0;
var btnSubmit = document.getElementById("btnSubmit");
btnSubmit.onclick = function () {
t.value = head.style.top.replace("px","");
l.value = head.style.left.replace("px","");
return true;
}
var img = head.getElementsByTagName("img")[0];
img.onload = function () {
img_width = img.width;
img_height = img.height;
head.style.width = img_width + "px";
head.style.height = img_height + "px";
img.style.display = "none";
head.style.background = "url(" + img.attributes["src"].nodeValue + ") no-repeat ";
head.style.backgroundSize = "100px 75px";
}
head.onmousedown = function (e) {
var old_x = e.clientX;
var old_y = e.clientY;
head.style.cursor = "move";
document.onmousemove = function (e) {
var x = e.clientX;
var y = e.clientY;
position_x += x - old_x;
position_y += y - old_y;
head.style.left = position_x + "px";
head.style.top = position_y + "px";
old_x = x;
old_y = y;
}
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
head.style.cursor = "default";
}
}
})();
</script>
</body>
</html>
<%@ WebHandler Language="C#" Class="Data" %>
using System;
using System.Collections.Generic;
using System.Web;
public class Data : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
protected HttpRequest Request;
protected HttpResponse Response;
protected HttpServerUtility Server;
public void ProcessRequest(HttpContext context)
{
Request = context.Request;
Response = context.Response;
Server = context.Server;
Response.ContentType = "text/plain";
string avatorFilePath = Server.MapPath("img.jpg");
string animalFilePath = Server.MapPath("2.jpg");
int width = 0;
int height = 0;
int top = int.Parse(context.Request.Form["t"]);
int left = int.Parse(context.Request.Form["l"]);
using (System.Drawing.Image avator = System.Drawing.Image.FromFile(avatorFilePath, true))
{
width = avator.Width;
height = avator.Height;
}
using (System.Drawing.Image pickedImage = new System.Drawing.Bitmap(width, height))
{
using (System.Drawing.Graphics pickedG = System.Drawing.Graphics.FromImage(pickedImage))
{
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//加载背景
using (System.Drawing.Image avatorImage = System.Drawing.Image.FromFile(avatorFilePath, true))
{
System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, width, height);
System.Drawing.Rectangle toR = new System.Drawing.Rectangle(0, 0, width, height);
pickedG.DrawImage(avatorImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
}
using (System.Drawing.Image animalImage = System.Drawing.Image.FromFile(animalFilePath, true))
{
System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, animalImage.Width, animalImage.Height);
System.Drawing.Rectangle toR = new System.Drawing.Rectangle(left, top, animalImage.Width, animalImage.Height);
pickedG.DrawImage(animalImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
}
//关键质量控制
//获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
System.Drawing.Imaging.ImageCodecInfo[] icis = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo ici = null;
foreach (System.Drawing.Imaging.ImageCodecInfo i in icis)
{
if (i.MimeType == "image/jpeg")
{
ici = i;
}
}
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);
//保存缩略图
byte[] bytes;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
pickedImage.Save(ms, ici, ep);
bytes = ms.ToArray();
ms.Close();
ms.Dispose();
}
Response.OutputStream.Write(bytes, 0, bytes.Length);
}
}
Response.CacheControl = "no-cache";
Response.ContentType = "image/jpeg";
}
}