=波波日志 > Asp.Net/C#/WCF > GDI+ 中发生一般性错误=

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();
    }
}
类别:Asp.Net/C#/WCF 作者:波波 日期:2010-07-09 【评论:0】 
 
暂时没有评论!
发表留言
  • *昵称:
  • 头像:
  • 电子邮件: [留下您的邮件,方便管理员回复您。]
  • 个人网站: *验证码:
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,©2012WEB编程网桂ICP备05005887号 京公网安备1101055090