欢迎光临
我们一直在努力

网站裁剪照片源码

客户端js不能操作文件,所以只能先上传图片再在服务器端剪切。

1、上传图片

2、js剪切图片(其实只是选取要剪切的部分)

3、服务器端剪切

     (1)在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。

其中剪切图片源码:

using system;  
using System.Collections.Generic;  
using System.Text;  
using System.Drawing;  
 
public class Cut  
{  
    /// <summary>  
    /// 裁剪图片  
    /// </summary>  
    /// <param name="sourceImg">原图片路径</param>  
    /// <param name="desImg">裁剪图片路径</param>  
    /// <param name="left">X</param>  
    /// <param name="top">Y</param>  
    /// <param name="width">宽</param>  
    /// <param name="height">高</param>  
    public static void CutImage(string sourceImg, string desImg, int left, int top, int width, int height)  
    {  
        System.Drawing.Image img = System.Drawing.Bitmap.FromFile(sourceImg);  
        System.Drawing.Image imgToSave = new System.Drawing.Bitmap(width, height);  
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgToSave);  
        RectangleF sourceRect = new RectangleF(left, top, width, height);  
        RectangleF destinationRect = new RectangleF(0, 0, width, height);  
 
        g.DrawImage(img,  
                    destinationRect,  
                    sourceRect,  
                    GraphicsUnit.Pixel  
                    );  
        g.Save();  
        imgToSave.Save(desImg, System.Drawing.Imaging.ImageFormat.Jpeg);  
        g.Dispose();  
        imgToSave.Dispose();  
        img.Dispose();  
    }  
 
 
}  
 
     (2)在ashx中剪切,可回传文件流。用参数传递坐标。  
 
Code  
 
using System;  
using System.Web;  
using System.Drawing;  
using System.IO;  
 
public class ImgCropper_WebHandler : IHttpHandler  
{  
    public void ProcessRequest(Httpcontext context)  
    {  
        string Pic = Convert.ToString(context.Request["p"]);  
        int PointX = Convert.ToInt32(context.Request["x"]);  
        int PointY = Convert.ToInt32(context.Request["y"]);  
        int CutWidth = Convert.ToInt32(context.Request["w"]);  
        int CutHeight = Convert.ToInt32(context.Request["h"]);  
        int PicWidth = Convert.ToInt32(context.Request["pw"]);  
        int PicHeight = Convert.ToInt32(context.Request["ph"]);  
 
        context.Response.ContentType = "image/jpeg";  
        ResetImg(context, System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream);  
    }  
 
    public MemoryStream ResetImg(HttpContext context, string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)  
    {  
        Image imgPhoto = Image.FromFile(ImgFile);  
        Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);  
 
        Graphics gbmPhoto = Graphics.FromImage(bmPhoto);  
        gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight, GraphicsUnit.Pixel);  
 
        //保存图片到服务器  
        bmPhoto.Save(context.Server.MapPath("upload/") + Guid.NewGuid() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);  
 
        //生成文件流回传  
        MemoryStream ms2 = new MemoryStream();  
        bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);  
 
        imgPhoto.Dispose();  
        gbmPhoto.Dispose();  
        bmPhoto.Dispose();  
 
        return ms2;  
    }  
 
 
    public bool IsReusable  
    {  
        get 
        {  
            return false;  
        }  
    }  
}

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《网站裁剪照片源码》
文章链接:https://www.456zj.com/9220.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址