0

我希望有人帮助我解决这个问题..

我有简单的 aspx 代码来从目录中获取图像文件,并在其上进行一些处理,以在其上绘制一些东西,然后将其保存在同一目录中。

确实代码在本地机器上运行良好,但在服务器端代码失败并出现异常。(ArgumentException:参数无效)

请看代码:

DirectoryInfo[] dir = new DirectoryInfo[2];
dir[0] = new DirectoryInfo(Server.MapPath("Image/DB/Large/"));
dir[1] = new DirectoryInfo(Server.MapPath("Image/DB/Thumb/"));

System.Drawing.Image signature = System.Drawing.Image.FromFile(Server.MapPath("Image/Design/signature.png"));
for (int i = 0; i < dir.Length; i++)
{
    FileInfo[] fs = dir[i].GetFiles("*.jpg");
    foreach (FileInfo s in fs)
    {
        FileStream strm = s.OpenRead();
        String name = s.Name;
        System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
        Graphics g = Graphics.FromImage(img);
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.DrawImage(signature, new Point(0, 0));       
        strm.Close();
        if (i == 0)
        {
            String v = Server.MapPath("Image/DB/Large/" + name);
            img.Save(v);
        }
        else if (i == 1)
        {
            String v = Server.MapPath("Image/DB/Slide/" + name);
            img.Save(v);
        }
        g.Dispose();         
    }
}

异常详情:

参数无效。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:参数无效。

  Source Error:

  An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

  Stack Trace:


[ArgumentException: Parameter is not valid.]
   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +1062843
   System.Drawing.Image.FromStream(Stream stream) +8
   Developer.Button1_Click(Object sender, EventArgs e) +279
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
4

1 回答 1

3

您必须将 FileStream 转换为 Stream

System.Drawing.Image.FromStream(..);// This method take Stream as argument and nor FileStream

您可以尝试使用 CopyTo 进行转换。

链接:http: //msdn.microsoft.com/en-us/library/dd782932.aspx

FileStream strm = s.OpenRead();
Stream stream = new Stream();
strm.CopyTo(stream);
System.Drawing.Image.FromStream(stream);

在.Net 4之前,可以使用这种扩展方式

公共静态类分机{

public static void CopyTo(this FileStream in, Stream out)
{
    byte[] temp= new byte[16*1024]; //You can adjust this value
    int bytesRead;

    while ((bytesRead = in.Read(temp,0, temp.Length)) > 0)
    {
        out.Write(temp, 0, bytesRead);
    }
}

}

于 2012-09-25T18:40:49.237 回答