System.ArgumentException when the code gets the stream of the image file - ASPX

advertisements

i hope anyone to help me in this code that i don't know where,what and why this problem has tookplace !!

i have this code responsible to get images from directory to make a simple watermark for each one, but there is an exception when the code arrive to read the image as streams..

please look at this code :

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

Image signature = 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));
        g.Dispose();

        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);
        }
    }
}

Excception Details :

[ArgumentException: .] System.Drawing.Image.FromStream(Stream stream, Boolean
 useEmbeddedColorManagement, Boolean validateImageData) +1065883
 System.Drawing.Image.FromStream(Stream stream) +8 Developer.Page_Load(Object sender,
 EventArgs e) in f:\.NET Programming\‫FaieqSahwish_V.2.0\Developer.aspx.cs:29
 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t,
 EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
 EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99
 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean
 includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


According to the FromStream documentation a ArgumentException is thrown if the stream is not a valid image format or if the stream is null.

Have you verified that neither of these conditions are met for the stream passed into FromStream?