使用.net裁剪网站上传的图片的方法
作者:bea
客户端Javascript不能操作文件,所以只能先上传图片再在服务器端剪切。 1、上传图片 2、Javascript剪切图片(其实只是选取要剪切的部分) 3、服务器端剪切 (1)在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。 其中剪切图片源码: usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Drawing; publicclass
客户端Javascript不能操作文件,所以只能先上传图片再在服务器端剪切。 1、上传图片 2、Javascript剪切图片(其实只是选取要剪切的部分) 3、服务器端剪切 (1)在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。 其中剪切图片源码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
publicclassCut
{
///
///裁剪图片
///
///原图片路径
///裁剪图片路径
///X
///Y
///宽
///高
publicstaticvoidCutImage(stringsourceImg,stringdesImg,intleft,inttop,intwidth,intheight)
{
System.Drawing.Imageimg=System.Drawing.Bitmap.FromFile(sourceImg);
System.Drawing.ImageimgToSave=newSystem.Drawing.Bitmap(width,height);
System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(imgToSave);
RectangleFsourceRect=newRectangleF(left,top,width,height);
RectangleFdestinationRect=newRectangleF(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
usingSystem;
usingSystem.Web;
usingSystem.Drawing;
usingSystem.IO;
publicclassImgCropper_WebHandler:IHttpHandler
{
publicvoidProcessRequest(HttpContextcontext)
{
stringPic=Convert.ToString(context.Request["p"]);
intPointX=Convert.ToInt32(context.Request["x"]);
intPointY=Convert.ToInt32(context.Request["y"]);
intCutWidth=Convert.ToInt32(context.Request["w"]);
intCutHeight=Convert.ToInt32(context.Request["h"]);
intPicWidth=Convert.ToInt32(context.Request["pw"]);
intPicHeight=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);
}
publicMemoryStreamResetImg(HttpContextcontext,stringImgFile,intPicWidth,intPicHeight,intPointX,intPointY,intCutWidth,intCutHeight)
{
ImageimgPhoto=Image.FromFile(ImgFile);
BitmapbmPhoto=newBitmap(CutWidth,CutHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GraphicsgbmPhoto=Graphics.FromImage(bmPhoto);
gbmPhoto.DrawImage(imgPhoto,newRectangle(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);
//生成文件流回传
MemoryStreamms2=newMemoryStream();
bmPhoto.Save(ms2,System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
gbmPhoto.Dispose();
bmPhoto.Dispose();
returnms2;
}
publicboolIsReusable
{
get
{
returnfalse;
}
}
} 有用 | 无用
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
publicclassCut
{
///
///裁剪图片
///
///原图片路径
///裁剪图片路径
///X
///Y
///宽
///高
publicstaticvoidCutImage(stringsourceImg,stringdesImg,intleft,inttop,intwidth,intheight)
{
System.Drawing.Imageimg=System.Drawing.Bitmap.FromFile(sourceImg);
System.Drawing.ImageimgToSave=newSystem.Drawing.Bitmap(width,height);
System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(imgToSave);
RectangleFsourceRect=newRectangleF(left,top,width,height);
RectangleFdestinationRect=newRectangleF(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
usingSystem;
usingSystem.Web;
usingSystem.Drawing;
usingSystem.IO;
publicclassImgCropper_WebHandler:IHttpHandler
{
publicvoidProcessRequest(HttpContextcontext)
{
stringPic=Convert.ToString(context.Request["p"]);
intPointX=Convert.ToInt32(context.Request["x"]);
intPointY=Convert.ToInt32(context.Request["y"]);
intCutWidth=Convert.ToInt32(context.Request["w"]);
intCutHeight=Convert.ToInt32(context.Request["h"]);
intPicWidth=Convert.ToInt32(context.Request["pw"]);
intPicHeight=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);
}
publicMemoryStreamResetImg(HttpContextcontext,stringImgFile,intPicWidth,intPicHeight,intPointX,intPointY,intCutWidth,intCutHeight)
{
ImageimgPhoto=Image.FromFile(ImgFile);
BitmapbmPhoto=newBitmap(CutWidth,CutHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GraphicsgbmPhoto=Graphics.FromImage(bmPhoto);
gbmPhoto.DrawImage(imgPhoto,newRectangle(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);
//生成文件流回传
MemoryStreamms2=newMemoryStream();
bmPhoto.Save(ms2,System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
gbmPhoto.Dispose();
bmPhoto.Dispose();
returnms2;
}
publicboolIsReusable
{
get
{
returnfalse;
}
}
} 有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 代码生成工具在Java开发中的作用
- Java编程如何节省内存效率高
- 何时被创建Java对象实例
- 怎样确保无线J2ME的安全
- this关键字在Java中怎么用
- Java中栈内存和堆内存入门
- 实例讲解Java对象初始化
- Java web开发中常遇的一些概念
- J2EE学习中一些值得研究的开源项目
- Java垃圾回收算法与内存泄露
- 异常处理机制在Java中的优缺点
- java入门学习之如何快速学习Java
- Java入门学习之选择开发的工具
- 转义字符 - XML中的非法字符
- 未组织好 - XML解析错误解决办法
- 几种实现asp.net模板生成html静态页面方法
- asp.net错误提示“服务器应用程序不可用”
- C#操作XML 读XML 写XML等
- 最新编程语言排行榜已高调出现