欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net实现裁剪网站上传图片的方法

程序员文章站 2024-02-20 22:28:58
本文实例讲述了基于.net实现裁剪网站上传图片的方法。由于客户端javascript不能操作文件,所以只能先上传图片再在服务器端剪切。 1、上传图片 2、javascr...

本文实例讲述了基于.net实现裁剪网站上传图片的方法。由于客户端javascript不能操作文件,所以只能先上传图片再在服务器端剪切。
1、上传图片
2、javascript剪切图片(其实只是选取要剪切的部分)
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中剪切,可回传文件流。用参数传递坐标。   

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;  
    }  
  }  
}