推荐一个asp.net的图片美化上传接口(美图秀秀)
这个其实是加载的美图秀秀的Api,使用的他们的JS和他们的FAlsh。编辑完之后可以直接上传到服务器
先来看看效果打开页面只有一个A链接
单击这个上传图片
选择两张图片
好了之后单击保存就行了。
这只是之一,里面还有好多种比如头像编辑,和图片美化
使用非常 方便
如果在家感觉他们的服务器不够快,可以使用自己的,只要下载Js类就行了。
代码如下
Html
[HTML] 纯文本查看 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>美图WEB开放平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://open.web.meitu.com/sources/xiuxiu.js" type="text/javascript"></script>
<script src="../Script/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
function meituload() {
// 1是美化图片,2拼图,5头像编辑,
xiuxiu.embedSWF("altContent", 2, "100%", "100%");
/*第1个参数是加载编辑器div容器,第2个参数是编辑器类型,第3个参数是div容器宽,第4个参数是div容器高*/
xiuxiu.setUploadURL("http://www.sufeinet.com/stream.ashx"); //修改为您自己的上传接收图片程序
xiuxiu.onInit = function () {
xiuxiu.loadPhoto("http://open.web.meitu.com/sources/images/1.jpg");
}
xiuxiu.onUploadResponse = function (data) {
//alert("上传响应" + data); 可以开启调试
}
xiuxiu._close = function () {
$("#meitubox").hide(); //单关闭和取消按钮时
}
}
</script>
<style type="text/css">
html, body { height: 100%; overflow: hidden; }
body { margin: 0; }
</style>
</head>
<body>
<a id="ameitu" href="#">上传图片</a>
<div id="meitubox" style="height: 100%;">
<div id="altContent">
</div>
</div>
</body>
</html>
还有一个Handler文件
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using System.IO;
using System.Drawing;
using XiuXiuWeb.XiuXiuStream;
namespace XiuXiuWeb
{
/// <summary>
/// Summary description for stream
/// </summary>
public class stream : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//config 配置节点可以将图片保存至指定目录,未配置将保存至 /XiuXiuUpload
//<appSettings>
// <add key="XiuXiuImageSavePath" value="/upload"/>
//</appSettings>
string name = null;
if (context.Request.TotalBytes > 0)
{
XiuXiuStreamImage img = new XiuXiuStreamImage(context);
name = img.Save();
}
else
{
name = "非法访问";
}
context.Response.ContentType = "text/plain";
context.Response.Write(name);
}
public bool IsReusable
{
get
{
return false;
}
}
}
namespace XiuXiuStream
{
/// <summary>
/// 上传抽象类
/// </summary>
public abstract class XiuXiuImage
{
/// <summary>
/// 基类保存
/// </summary>
/// <returns>返回保存路径+文件名</returns>
public virtual string Save()
{
string fileName = this.GetFileName();
if (null == fileName) return null;
string root = HttpContext.Current.Server.MapPath(path);
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
this.FileName = Path.Combine(root, fileName);
string[] paths = { path, fileName };
return string.Join("/", paths);
}
public XiuXiuImage()
{
path = path == null ? "/XiuXiuUpload" : path;
}
/// <summary>
/// 确定上传类型
/// </summary>
protected bool IsUplodType
{
get
{
string extension = this.GetExtension();
return ".jpg .gif .png .icon .bmp .tiff .wmf .emf .exif".IndexOf(extension) >= 0 ? true : false;
}
}
private string _fileName = null;
/// <summary>
/// 最终保存路径
/// </summary>
protected string FileName
{
set { _fileName = value; }
get { return _fileName; }
}
/// <summary>
/// 配置文件路径 无配置上传到XiuXiuUpload
/// </summary>
protected string path = ConfigurationManager.AppSettings["XiuXiuImageSavePath"];
/// <summary>
/// 获取拓展名
/// </summary>
/// <returns></returns>
protected abstract string GetExtension();
/// <summary>
/// 获取最终保存文件名
/// </summary>
/// <returns></returns>
protected string GetFileName()
{
string extension = this.GetExtension();
if (null == extension) return null;
else
{
string name = this.GetName();
string[] imgName = { "XiuXiu", name, extension };
return string.Join("", imgName);
}
}
/// <summary>
/// 获取保存文件名
/// </summary>
/// <returns></returns>
private string GetName()
{
DateTime uploadTime = DateTime.Now;
string[] times = { uploadTime.Year.ToString(), uploadTime.Month.ToString(), uploadTime.Day.ToString(),
uploadTime.Hour.ToString(), uploadTime.Millisecond.ToString(), uploadTime.Second.ToString() };
return string.Join("", times);
}
}
/// <summary>
/// Stream接收
/// </summary>
public sealed class XiuXiuStreamImage : XiuXiuImage
{
private MemoryStream stream = null;
public XiuXiuStreamImage(HttpContext context)
{
int count = context.Request.TotalBytes;
if (count > 0)
{
if (count < 1024 * 1024 * 5)
{
byte[] bytes = context.Request.BinaryRead(context.Request.TotalBytes);
this.stream = new MemoryStream(bytes);
}
}
}
private Image File
{
get
{
return this.stream == null ? null : Image.FromStream(this.stream);
}
}
/// <summary>
/// 保存图片,成功返回文件路径,失败null
/// 非图片格式返回错误信息
/// </summary>
/// <returns>成功返回文件路径 失败null</returns>
public override string Save()
{
if (!this.IsUplodType)
{
this.stream.Dispose();
return "Only allowed to upload pictures.";
}
string returnName = base.Save();
if (this.FileName != null)
{
this.File.Save(this.FileName);
this.stream.Dispose();
return returnName;
}
return null;
}
protected override string GetExtension()
{
if (this.File != null)
{
string fileExtension = this.File.RawFormat.ToString().Substring(14),
jpgExtension = System.Drawing.Imaging.ImageFormat.Jpeg.Guid.ToString(),
gifExtension = System.Drawing.Imaging.ImageFormat.Gif.Guid.ToString(),
pngExtension = System.Drawing.Imaging.ImageFormat.Png.Guid.ToString(),
iconExtension = System.Drawing.Imaging.ImageFormat.Icon.Guid.ToString(),
bmpExtension = System.Drawing.Imaging.ImageFormat.Bmp.Guid.ToString(),
tiffExtension = System.Drawing.Imaging.ImageFormat.Tiff.Guid.ToString(),
wmfExtension = System.Drawing.Imaging.ImageFormat.Wmf.Guid.ToString(),
emfExtension = System.Drawing.Imaging.ImageFormat.Emf.Guid.ToString(),
exifExtension = System.Drawing.Imaging.ImageFormat.Exif.Guid.ToString();
fileExtension = fileExtension.Substring(0, fileExtension.Length - 1);
if (fileExtension == jpgExtension)
{
return ".jpg";
}
else if (fileExtension == gifExtension)
{
return ".gif";
}
else if (fileExtension == pngExtension)
{
return ".png";
}
else if (fileExtension == iconExtension)
{
return ".icon";
}
else if (fileExtension == bmpExtension)
{
return ".bmp";
}
else if (fileExtension == tiffExtension)
{
return ".tiff";
}
else if (fileExtension == wmfExtension)
{
return ".wmf";
}
else if (fileExtension == emfExtension)
{
return ".emf";
}
else if (fileExtension == exifExtension)
{
return ".exif";
}
}
return null;
}
}
}
}
需要的同志可以直接到官网上看看
http://open.web.meitu.com/products/
|