网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

交友类网站裁剪照片源码

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

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

1、上传图片

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

3、服务器端剪切

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

其中剪切图片源码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Drawing;  
  5.  
  6. public class Cut  
  7. {  
  8.     /// <summary>  
  9.     /// 裁剪图片  
  10.     /// </summary>  
  11.     /// <param name="sourceImg">原图片路径</param>  
  12.     /// <param name="desImg">裁剪图片路径</param>  
  13.     /// <param name="left">X</param>  
  14.     /// <param name="top">Y</param>  
  15.     /// <param name="width">宽</param>  
  16.     /// <param name="height">高</param>  
  17.     public static void CutImage(string sourceImg, string desImg, int left, int top, int width, int height)  
  18.     {  
  19.         System.Drawing.Image img = System.Drawing.Bitmap.FromFile(sourceImg);  
  20.         System.Drawing.Image imgToSave = new System.Drawing.Bitmap(width, height);  
  21.         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgToSave);  
  22.         RectangleF sourceRect = new RectangleF(left, top, width, height);  
  23.         RectangleF destinationRect = new RectangleF(0, 0, width, height);  
  24.  
  25.         g.DrawImage(img,  
  26.                     destinationRect,  
  27.                     sourceRect,  
  28.                     GraphicsUnit.Pixel  
  29.                     );  
  30.         g.Save();  
  31.         imgToSave.Save(desImg, System.Drawing.Imaging.ImageFormat.Jpeg);  
  32.         g.Dispose();  
  33.         imgToSave.Dispose();  
  34.         img.Dispose();  
  35.     }  
  36.  
  37.  
  38. }  
  39.  
  40.      (2)在ashx中剪切,可回传文件流。用参数传递坐标。  
  41.  
  42. Code  
  43.  
  44. using System;  
  45. using System.Web;  
  46. using System.Drawing;  
  47. using System.IO;  
  48.  
  49. public class ImgCropper_WebHandler : IHttpHandler  
  50. {  
  51.     public void ProcessRequest(HttpContext context)  
  52.     {  
  53.         string Pic = Convert.ToString(context.Request["p"]);  
  54.         int PointX = Convert.ToInt32(context.Request["x"]);  
  55.         int PointY = Convert.ToInt32(context.Request["y"]);  
  56.         int CutWidth = Convert.ToInt32(context.Request["w"]);  
  57.         int CutHeight = Convert.ToInt32(context.Request["h"]);  
  58.         int PicWidth = Convert.ToInt32(context.Request["pw"]);  
  59.         int PicHeight = Convert.ToInt32(context.Request["ph"]);  
  60.  
  61.         context.Response.ContentType = "image/jpeg";  
  62.         ResetImg(context, System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream);  
  63.     }  
  64.  
  65.     public MemoryStream ResetImg(HttpContext context, string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)  
  66.     {  
  67.         Image imgPhoto = Image.FromFile(ImgFile);  
  68.         Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);  
  69.  
  70.         Graphics gbmPhoto = Graphics.FromImage(bmPhoto);  
  71.         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);  
  72.  
  73.         //保存图片到服务器  
  74.         bmPhoto.Save(context.Server.MapPath("upload/") + Guid.NewGuid() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);  
  75.  
  76.         //生成文件流回传  
  77.         MemoryStream ms2 = new MemoryStream();  
  78.         bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);  
  79.  
  80.         imgPhoto.Dispose();  
  81.         gbmPhoto.Dispose();  
  82.         bmPhoto.Dispose();  
  83.  
  84.         return ms2;  
  85.     }  
  86.  
  87.  
  88.     public bool IsReusable  
  89.     {  
  90.         get 
  91.         {  
  92.             return false;  
  93.         }  
  94.     }  
  95. }  
  • 下一篇资讯: .NET3.0资源列表
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 1280 x 720 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师