下载远程图片并压缩到指定大小加水印,并生成缩略图
调用 partImg.Save()时出错“GDI+ 中发生一般性错误。”调试代码如下
bigimg = "http://i2.3conline.com/images/pcautogallery/20099/20/st/9717/80_bthumb.JPG";
                        int pPartWidth, pPartHeight, pPartStartPointX, pPartStartPointY, pOrigStartPointX, pOrigStartPointY;
                        string normalJpgPath = "/SiteImg/" + DateTime.Now.ToString("hhmmssff") + ".jpg";
                        pPartStartPointX = 0;
                        pPartStartPointY = 0;
                        pOrigStartPointX = 0;
                        pOrigStartPointY = 0;
                        
                        System.Net.WebClient client = new System.Net.WebClient();                       
                        System.IO.Stream stm = client.OpenRead(bigimg);
                        System.Drawing.Image originalImg = System.Drawing.Image.FromStream(stm, true);
                        pPartWidth = originalImg.Width;
                        pPartHeight = originalImg.Height;
                        System.Drawing.Bitmap partImg = new System.Drawing.Bitmap(pPartWidth, pPartHeight);
                        System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(partImg);
                        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(new System.Drawing.Point(pPartStartPointX, pPartStartPointY), new System.Drawing.Size(pPartWidth, pPartHeight));//目标位置
                        System.Drawing.Rectangle origRect = new System.Drawing.Rectangle(new System.Drawing.Point(pOrigStartPointX, pOrigStartPointY), new System.Drawing.Size(pPartWidth, pPartHeight));
                        graphics.DrawImage(originalImg, destRect, origRect, System.Drawing.GraphicsUnit.Pixel);
                        graphics.DrawImage(originalImg, 0, 0);                        originalImg.Dispose();
                        if (System.IO.File.Exists(normalJpgPath))
                        {
                            System.IO.File.SetAttributes(normalJpgPath, System.IO.FileAttributes.Normal);
                            System.IO.File.Delete(normalJpgPath);
                        }
                        partImg.Save(normalJpgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
注:我试过先把原图保存到服务器后,再调用保存的图片处理是可以的,但我不想这样做。我想真接能去掉别人的水印后,压缩缩略图和加水印

解决方案 »

  1.   

    相应的帐户没有写权限。  
    指定的物理路径不存在。   
    保存的文件已存在并因某种原因被锁定。  
    在代码中使用 using 语句,释放 Image 对象所使用的所有资源。 
      

  2.   


    相应的帐户没有写权限。  (已是可修改权限了)
    指定的物理路径不存在。   (有这个文件夹,是对的)
    保存的文件已存在并因某种原因被锁定。  (没有锁定,因为我直接用Client.downfile是可以的)
    在代码中使用 using 语句,释放 Image 对象所使用的所有资源。 (这个方法里没有用using)
      

  3.   

    partImg.Dispose(); 画布对象关闭.
    Graphics..Dispose(); 对象关闭
      

  4.   

    client.DownloadFile() 调用这个方法直接把原图保存在服务器是可以的,所以这个应不是权限问题吧。
      

  5.   

    //少了Server.MapPath将虚拟路径转成物理路径
    string normalJpgPath = Server.MapPath("/SiteImg/" + DateTime.Now.ToString("hhmmssff") + ".jpg");