哪位有上传和保存图片的代码发一份请给我,谢谢,[email protected]

解决方案 »

  1.   

     protected void UpPicture()
            {
                if (FileUpload1.FileName.ToString() == "")
                {
                    model_news.ImgURL = "";                
                    
                }
                else
                {
                    model_news.ImgURL = Guid.NewGuid().ToString() + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("."));
                    string Picturepath = "/UploadFile/news/Picture/";//原图片存储地址
                    string SmallPicturepath = "/UploadFile/news/SmallPicture/";//缩微图片存储地址
                    string CharaterPicturepath = "/UploadFile/news/CharaterPicture/";//带文字水印的图片存储地址
                    string WaterImgPicturepath = "/UploadFile/news/WaterImgPicture/";//带图片水印的图片存储地址
                    WaterImage wi = new WaterImage();
                    wi.waterImage(FileUpload1, model_news.ImgURL, Picturepath, SmallPicturepath, CharaterPicturepath, WaterImgPicturepath, "盈通平台系统", 120, 90, 0.5, 0.5, 0.5, 0.5, false, false, false);
                }
            }#region 生成图片水印和缩略图
            /// <summary>
            /// 生成图片水印和缩略图
            /// </summary>
            /// <param name="fu">FileUpload's ID</param>  
            /// /// <param name="ImageName">保存图片的名字</param> 
            /// <param name="serverPath">文件原图存放路径</param>
            /// <param name="ThumbnailPath">文件缩略图存放路径</param>
            /// <param name="waterPath"> 服务器端文字水印图存放路径(文字水印)</param>
            /// <param name="waterImgSave">服务器端图片水印图存放路径(图片水印)</param>
            /// <param name="width">缩略图宽度</param>
            /// <param name="height">缩略图高度</param>
            /// <param name="xPercent">水印文字与图片左上角X轴的百分比(用小数表示)</param>
            /// <param name="yPercent">水印文字与图片左上角Y轴的百分比(用小数表示)</param>
            /// <param name="xPer">水印图片与图片左上角X轴的百分比(用小数表示)</param>
            /// <param name="yPer">水印图片与图片左上角Y轴的百分比(用小数表示)</param>
            /// <param name="isWaterImg">是否生成图片水印</param>
            /// <param name="isCharaterImage">是否生成文字水印</param>
            /// <param name="isMakeThumbnail">是否生成缩略图</param>
            /// <returns></returns>
            public string waterImage(System.Web.UI.WebControls.FileUpload fu,string ImageName, string serverPath, string ThumbnailPath, string waterPath, string waterImgSave, string waterCharater, int width, int height, double xPercent, double yPercent, double xPer, double yPer, bool isWaterImg, bool isCharaterImage, bool isMakeThumbnail)
            {
                if (fu.HasFile)
                {
                    string fileContentType = fu.PostedFile.ContentType;
                    if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                    {
                        string name = fu.PostedFile.FileName;// 客户端文件路径
                        FileInfo file = new FileInfo(name);
                        //string fileName = randName + file.Name;// 文件名称
                        //string fileName_s = "small_" + randName + file.Name;// 缩略图文件名称
                        //string fileName_sy = "text_" + randName + file.Name;// 水印图文件名称(文字)
                        //string fileName_syp = "water_" + randName + file.Name;// 水印图文件名称(图片)                  // string fileContentType2 = fu.PostedFile.FileName.Substring(fu.PostedFile.FileName.LastIndexOf("."));                    string fileName = ImageName;// 文件名称
                        string fileName_s = "small_" + ImageName;// 缩略图文件名称
                        string fileName_sy = "text_" + ImageName;// 水印图文件名称(文字)
                        string fileName_syp = "water_" + ImageName;// 水印图文件名称(图片)                    string yt = serverPath + fileName;// 原图片存放路径
                        string slt = ThumbnailPath + fileName_s;// 服务器端缩略图存放路径
                        string wzsyt = waterPath + fileName_sy;// 服务器端文字水印图存放路径
                        string tpsyt = waterImgSave + fileName_syp;//服务器端图片水印图存放路径                    string webFilePath = HttpContext.Current.Server.MapPath(yt);
                        string webFilePath_s = HttpContext.Current.Server.MapPath(slt);
                        string webFilePath_sy = HttpContext.Current.Server.MapPath(wzsyt);
                        string webFilePath_syp = HttpContext.Current.Server.MapPath(tpsyt);
                        string webFilePath_sypf = HttpContext.Current.Server.MapPath("/UploadFile/waterImg.gif");//服务器端水印图路径(图片)
                        //if (!File.Exists(webFilePath))
                        //{
                        //    try
                        //    {
                        fu.SaveAs(webFilePath);  //保存原图片  
                        if (isCharaterImage)
                        {
                            AddWater(webFilePath, webFilePath_sy, waterCharater, xPercent, yPercent);//添加文字水印
                        }
                        else
                        {
                            //fu.SaveAs(HttpContext.Current.Server.MapPath(wzsyt)); //直接保存,不加文字水印
                        }
                        if (isWaterImg)
                        {
                            AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf, xPer, yPer);//添加图片水印
                        }
                        else
                        {
                            //fu.SaveAs(HttpContext.Current.Server.MapPath(tpsyt));
                        }
                        if (isMakeThumbnail)
                        {
                            MakeThumbnail(webFilePath, webFilePath_s, width, height, "Cut"); //生成缩略图
                        }
                        else
                        {
                            //fu.SaveAs(HttpContext.Current.Server.MapPath(slt));
                        }
                        return yt + "|" + slt + "|" + wzsyt + "|" + tpsyt;
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        return "fail1";
                        //    }
                        //}
                        //else
                        //{
                        //    return "fail2";                    //}
                    }
                    else
                    {
                        return "fail3";
                    }
                }
                else
                {
                    return "sucess";
                }
            }
            #endregion