using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.IO; namespace Itlead.Architecture.WebAppUI.tools { /// /// CovertPic 的摘要说明 /// public class CovertPic : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); string _fileNamePath = ""; _fileNamePath = context.Request.Params["PicName"]; var path = ""; path = CovertImage(_fileNamePath); context.Response.Write(path); } private string CovertImage(string FilePath) { Bitmap bt; //声明一个转换图片格式的bitmap对象 string OP;//声明一个变量用于存储ConvertImage中原始图片的路径 string savePath = ""; string Paths = ""; var FileName = ""; try { string serverPath = System.Web.HttpContext.Current.Server.MapPath(FilePath); string ImgName = serverPath.Substring(serverPath.LastIndexOf("\\") + 1, serverPath.Length - serverPath.LastIndexOf("\\") - 1); savePath = serverPath.Substring(0, serverPath.LastIndexOf("\\") + 1); ImgName = ImgName.Remove(ImgName.LastIndexOf("."));//获取图片名称不带扩展名 OP = serverPath.ToString();//获取图片所在路径 bt = new Bitmap(OP); FileName = ImgName + ".png"; Paths = savePath + "\\" + FileName;//设置保存路径 if (File.Exists(Paths)) { File.Delete(Paths); } bt.Save(Paths, System.Drawing.Imaging.ImageFormat.Png);//保存 bt.Dispose(); return FileName; } catch { return ""; } } public bool IsReusable { get { return false; } } } }