分享一个用于替换复制网页当中的图片地址并保存在服务器的上方法

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

  

posted @ 2013-05-23 14:53  Care健康  阅读(521)  评论(3编辑  收藏  举报
版权
作者:Bober Song

出处:http://bober.cnblogs.com

Care健康:http://www.aicareyou.com

推荐空间:华夏名网

本文首发博客园,版权归作者跟博客园共有。

转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。