GDI+ 中发生一般性错误

  在使用System.Drawing.Bitmap.Save保存图片时,图片路径需要为物理路径,要不就会出现“GDI+ 中发生一般性错误”的错误提示,Save方法不接收虚拟路径,参数为物理路径,要使用Server.MapPath将虚拟路径转换成物理路径。


  下面是一个在csdn上看到的图片采集功能功能,由于使用了虚拟路径,所以出现了GDI+ 中发生一般性错误的问题,转换为物理路径后就可以了。

  当然出现个问题可能为权限问题,目录不存在等的问题。

  下面为CSDN源代码
+展开
-C#
using System;
using System.IO;
using System.Net;
using System.Drawing;
public partial class img : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string bigimg = "http://i2.3conline.com/images/pcautogallery/20099/20/st/9717/80_bthumb.JPG";


        int pPartWidth, pPartHeight, pPartStartPointX, pPartStartPointY, pOrigStartPointX, pOrigStartPointY;
        string normalJpgPath = Server.MapPath("/SiteImg/" + DateTime.Now.ToString("hhmmssff") + ".jpg");
        //下面的没转成物理路径,所以出错
        //string normalJpgPath ="/SiteImg/" + DateTime.Now.ToString("hhmmssff") + ".jpg";

        pPartStartPointX = 0;
        pPartStartPointY = 0;
        pOrigStartPointX = 0;
        pOrigStartPointY = 0;

        WebClient client = new System.Net.WebClient();


        Stream stm = client.OpenRead(bigimg);
        Image originalImg = Image.FromStream(stm, true);
        pPartWidth = originalImg.Width;
        pPartHeight = originalImg.Height;
        Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
        Graphics graphics = Graphics.FromImage(partImg);
        Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
        Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));


        graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
        graphics.DrawImage(originalImg, 0, 0);
        stm.Close();
        originalImg.Dispose();
        if (File.Exists(normalJpgPath))
        {
            File.SetAttributes(normalJpgPath, FileAttributes.Normal);
            File.Delete(normalJpgPath);
        }
        partImg.Save(normalJpgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    
        partImg.Dispose();
    }
}

加支付宝好友偷能量挖...


原创文章,转载请注明出处:GDI+ 中发生一般性错误

评论(0)Web开发网
阅读(225)喜欢(0)Asp.Net/C#/WCF