我在 asp.net 网页上有一项图像编辑工作。实际上,工作是我必须选择图像的区域,然后在图像的选定部分绘制一些图形对象,如黑色矩形框以隐藏选定的部分图像(与编辑工作类似)。最后必须将编辑工作保存回原始图像。
我已经在我的网页上加载了图像,还允许用户使用 javascript 函数在图像上绘制一些矩形框。
我还编写了将该图像保存回服务器的代码……但它不起作用……
我认为控件从未越过这条线(这条线可能有一些错误)
var image = document.getElementById("Image2").toDataURL("image/png");
为了检查的目的,我在上面的行之后放了一个警告声明......但它没有被打印出来......并且什么也没发生?......
用于在图像上绘制矩形框的 Java 脚本代码:
<head>
<style type="text/css">
#rubberBand
{
position: absolute;
visibility: hidden;
width:0px;
height:0px;
border: 2px;border-color:Red;
background-color:black;
}
</style>
</head>
<body>
<div id="rubberBand"></div>
<asp:Image ID="Image2" runat="server" Width="650px" Height="700px"/>
<script type="text/javascript">
var IMG;
function startRubber(evt) {
if (document.all) {
var r = document.all.rubberBand;
r.style.width = 0;
r.style.height = 0;
r.style.pixelLeft = event.x;
r.style.pixelTop = event.y;
r.style.visibility = 'visible';
IMG.ondragstart = cancelDragDrop; // otherwise IE will try to drag the image
}
else if (document.getElementById) {
// firefox
evt.preventDefault();
var r = document.getElementById('rubberBand');
r.style.width = 0;
r.style.height = 0;
r.style.left = evt.clientX + 'px';
r.style.top = evt.clientY + 'px';
r.style.visibility = 'visible';
r.onmouseup = stopRubber;
}
IMG.onmousemove = moveRubber;
}
function moveRubber(evt) {
if (document.all) { // IE
var r = document.all.rubberBand;
r.style.width = event.x - r.style.pixelLeft;
r.style.height = event.y - r.style.pixelTop;
}
else if (document.getElementById) { // firefox
var r = document.getElementById('rubberBand');
r.style.width = evt.clientX - parseInt(r.style.left);
r.style.height = evt.clientY - parseInt(r.style.top);
}
return false; // otherwise IE won't fire mouseup
}
function stopRubber(evt) {
IMG.onmousemove = null;
}
function cancelDragDrop() {
window.event.returnValue = false;
}
IMG = document.getElementById('Image2');
IMG.onmousedown = startRubber;
IMG.onmouseup = stopRubber;
</script>
</body>
这是我保存图像的 java 脚本代码:
<head>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
// Send the canvas image to the server.
$(function () {
$("#btnSend").click(function () {
var image = document.getElementById("Image2").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.ajax({
type: 'POST',
url: 'Default.aspx/UploadImage',
data: '{ "imageData" : "' + image + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert('Image sent!');
}
});
});
});
</script>
</head>
<body>
<asp:Image ID="Image2" runat="server" Width="650px" Height="700px"/>
<input id="btnSend" type="button" value="Send To Server" />
</body>
保存图片的后端代码:
[WebMethod()]
public static void UploadImage(string imageData)
{
FileStream fs = new FileStream("D:\\vs-2010projects\\js_save\\js_save\\myimages\\image.png", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}