分享一个用于替换复制网页当中的图片地址并保存在服务器的上方法
2013-05-23 14:53
585 查看
/// <summary> /// 替换文章中的图片,并保存下来。 /// </summary> /// <param name="content">内容</param> /// <param name="newImageList">新保存后的图片地址</param> /// <param name="folder">保存的根目录</param> /// <param name="path">保存的绝对地址</param> /// <returns>保存更新了图片地址的文章内容</returns> public static string ReplaceContentImageList(string content, out string[] newImageList, string folder, string path) { string[] imageList = GetHtmlImageUrlList(content); imageList = imageList.Distinct().ToArray();//去除重复的 newImageList = new string[imageList.Length]; int index = 0; if (imageList.Length > 0) { foreach (string img in imageList) { WebClient wc = new WebClient(); Stream stream = wc.OpenRead(img); string imgFormat = img.Substring(img.LastIndexOf(".")); using (Bitmap orginal = new Bitmap(stream)) { //orginal.Save("c://out.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); /*MemoryStream ms = new MemoryStream();*/ /*newimage.Save(ms, ImageFormat.Jpeg); */ //Response.ContentType = "image/Jpeg"; //newimage.Save(Response.OutputStream, ImageFormat.Jpeg); //string folder = "~/Content/" + DateTime.Now.Month.ToString(); //if (Directory.Exists(Server.MapPath(folder)) == false)//如果不存在就创建file文件夹 //{ // Directory.CreateDirectory(Server.MapPath(folder)); //} string fileName = Guid.NewGuid().ToString().Replace("-", "") + imgFormat; string filePath = path + "/" + fileName; string replaceImg = folder.Replace("~", "") + "/" + fileName; switch (imgFormat.ToLower()) { case ".jpg": case ".jpeg": orginal.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".gif": orginal.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif); break; case ".bmp": orginal.Save(filePath, System.Drawing.Imaging.ImageFormat.Bmp); break; case ".png": orginal.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); break; case ".icon": orginal.Save(filePath, System.Drawing.Imaging.ImageFormat.Icon); break; default: break; } newImageList[index++] = replaceImg; content = content.Replace(img, replaceImg); } } } return content; }
相关文章推荐
- 预加载2+网页加载条 可以直接复制代码运行,如果图片地址丢失的话可以自己替换掉
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- ASP自动解析网页中的图片地址,并将其保存到本地服务器http://epasser.aydc.com.cn/article/adp/2/content13959.html
- asp自动解析网页中的图片地址,并将其保存到本地服务器
- 分享PHP源码批量抓取远程网页图片并保存到本地的实现方法
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- 分享PHP源码批量抓取远程网页图片并保存到本地的实现方法
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- imageView显示保存在服务器的图片,不是网页上的图片
- 安装DZ时出现“UCenter 的URL地址不正确”错误的解决方案 和首页上部分插入flash图片调用的方法!非网上复制!
- Android将TextView中的文字替换为一个小图片的方法
- php通过网页图片地址将图片保存到本地
- 网页图片失效自动替换图片地址js代码
- 将文本文件的内容或者文字保存成图片的方法分享
- 快速保存网页中所有图片的方法
- summernote图片上传功能保存到服务器指定文件夹+php代码+java方法
- 一个图片地址分解程序(用于PHP小偷程序)